From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: redhat.com, ip: 209.132.183.28, mailfrom: lersek@redhat.com) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by groups.io with SMTP; Tue, 01 Oct 2019 08:31:24 -0700 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9702618CB8FA; Tue, 1 Oct 2019 15:31:23 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-121-64.rdu2.redhat.com [10.10.121.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id 879DE60C83; Tue, 1 Oct 2019 15:31:18 +0000 (UTC) Subject: Re: [edk2-devel] [PATCH wave 1 00/10] support QEMU's "SMRAM at default SMBASE" feature To: Igor Mammedov Cc: devel@edk2.groups.io, Ard Biesheuvel , Boris Ostrovsky , Brijesh Singh , Jiewen Yao , Joao M Martins , Jordan Justen , Jun Nakajima , Michael Kinney , Paolo Bonzini , Phillip Goerl , Yingwen Chen References: <20190924113505.27272-1-lersek@redhat.com> <20190927133533.14b75bc1@redhat.com> From: "Laszlo Ersek" Message-ID: <15679c12-857f-cdd2-47ae-adbfe21c2d57@redhat.com> Date: Tue, 1 Oct 2019 17:31:17 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20190927133533.14b75bc1@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.63]); Tue, 01 Oct 2019 15:31:23 +0000 (UTC) Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 09/27/19 13:35, Igor Mammedov wrote: > On Tue, 24 Sep 2019 13:34:55 +0200 > "Laszlo Ersek" wrote: >> Going forward, if I understand correctly, the plan is to populate the >> new SMRAM with the hotplug SMI handler. (This would likely be put in >> place by OvmfPkg/PlatformPei, from NASM source code. The >> SmmRelocateBases() function in UefiCpuPkg/PiSmmCpuDxeSmm backs up the >> original contents before, and restores them after, the initial SMBASE >> relocation of the cold-plugged VCPUs. Thus the hotplug SMI handler would >> survive the initial relocation of the cold-plugged VCPUs.) > that's the tricky part, can we safely detect (in SmmRelocateBases) > race condition in case of several hotplugged CPUs are executing > SMI relocation handler at the same time? (crashing system in case > that happens is good enough) Wait, there are *two* initial SMI handlers here, one for cold-plugged CPUs, and another for hot-plugged CPUs. Let's just call them coldplug and hotplug handlers, for simplicity. In chronological order, during boot: (1) OvmfPkg/PlatformPei would put the hotplug handler in place, at the default SMBASE. This code would lie dormant for a long time. (2) UefiCpuPkg/PiSmmCpuDxeSmm backs up the current contents of the default SMBASE area, and installs the coldplug handler. This handler is then actually used, for relocating SMBASE for the present (cold-plugged) CPUs. Finally, UefiCpuPkg/PiSmmCpuDxeSmm restores the original contents of the default SMBASE area. This would in effect re-establish the hotplug handler from (1). (3) At OS runtime, a CPU is hotplugged, and then the hotplug handler runs, at the default SMBASE. The function SmmRelocateBases() is strictly limited to step (2), and has nothing to do with hotplug. Therefore it need not deal with any race conditions related to multi-hotplug. This is just to clarify that your question applies to the initial SMI handler (the hotplug one) that is put in place in step (1), and then used in step (3). The question does not apply to SmmRelocateBases(). With that out of the way, I do think we should be able to detect this, with a simple -- haha, famous last words! -- compare-and-exchange (LOCK CMPXCHG); a kind of semaphore. The initial value of an integer variable in SMRAM should be 1. At the start of the hotplug initial SMI handler, the CPU should try to swap that with 0. If it succeeds, proceed. If it fails (the variable's value is not 1), force a platform reset. Finally, right before the RSM, restore 1 (swap it back). (It does not matter if another hotplug CPU starts the relocation in SMM while the earlier one is left with *only* the RSM instruction in SMM, immediately after the swap-back.) Alternatively, if it's friendlier or simpler, we can spin on the initial LOCK CMPXCHG until it succeeds, executing PAUSE in the loop body. That should be friendly with KVM too (due to pause loop exiting). >> Further, when a VCPU is hot-plugged, we seem to intend to raise an SMI >> for it (perhaps internally to QEMU?), > I'd rather rise SMI from guest side (using IO port write from > ACPI cpu hotplug handler). Which in typical case (QEMU negotiated > SMI broadcast) will trigger pending SMI on hotplugged CPU as well. > (if it's acceptable I can prepare corresponding QEMU patches) What if the OS does not send the SMI (from the GPE handler) at once, and boots the new CPU instead? Previously, you wrote that the new CPU "won't get access to privileged SMRAM so OS can't subvert firmware. The next time SMI broadcast is sent the CPU will use SMI handler at default 30000 SMBASE. It's up to us to define behavior here (for example relocation handler can put such CPU in shutdown state)." How can we discover in the hotplug initial SMI handler at 0x3_0000 that the CPU executing the code should have been relocated *earlier*, and the OS just didn't obey the ACPI GPE code? I think a platform reset would be a proper response, but I don't know how to tell, "this CPU should not be here". > In case of unicast SMIs, a CPU handling guest ACPI triggered > SMI, can send SMI to hotpluged CPU as an additional step. I think it's OK if we restrict this feature to "broadcast SMI" only. > >> which will remain pending until >> the VCPU is launched with INIT-SIPI-SIPI. Once the INIT-SIPI-SIPI is >> sent, the VCPU will immediately enter SMM, and run the SMI handler in >> the locked SMRAM at the default SMBASE. At RSM, it may continue at the >> vector requested in the INIT-SIPI-SIPI. > Firmware can arbitrate/send INIT-SIPI-SIPI to hotplugged CPUs. > Enumerating hotplugged CPUs, can be done by reusing CPU hotplug > interface described at QEMU/docs/specs/acpi_cpu_hotplug.txt. > (preferably in read-only mode) ... I'll have to look at that later :) Thanks Laszlo