xref: /openbmc/qemu/docs/system/cpu-hotplug.rst (revision ea8ae47bdd2024dc2596f16b27f27fd4dcc08776)
1392d8e95SPeter Maydell===================
2392d8e95SPeter MaydellVirtual CPU hotplug
3392d8e95SPeter Maydell===================
4392d8e95SPeter Maydell
5392d8e95SPeter MaydellA complete example of vCPU hotplug (and hot-unplug) using QMP
6392d8e95SPeter Maydell``device_add`` and ``device_del``.
7392d8e95SPeter Maydell
8392d8e95SPeter MaydellvCPU hotplug
9392d8e95SPeter Maydell------------
10392d8e95SPeter Maydell
11392d8e95SPeter Maydell(1) Launch QEMU as follows (note that the "maxcpus" is mandatory to
12392d8e95SPeter Maydell    allow vCPU hotplug)::
13392d8e95SPeter Maydell
14392d8e95SPeter Maydell      $ qemu-system-x86_64 -display none -no-user-config -m 2048 \
15392d8e95SPeter Maydell          -nodefaults -monitor stdio -machine pc,accel=kvm,usb=off \
16392d8e95SPeter Maydell          -smp 1,maxcpus=2 -cpu IvyBridge-IBRS \
17c2387413SDaniel P. Berrangé          -qmp unix:/tmp/qmp-sock,server=on,wait=off
18392d8e95SPeter Maydell
19392d8e95SPeter Maydell(2) Run 'qmp-shell' (located in the source tree, under: "scripts/qmp/)
20392d8e95SPeter Maydell    to connect to the just-launched QEMU::
21392d8e95SPeter Maydell
22392d8e95SPeter Maydell      $> ./qmp-shell -p -v /tmp/qmp-sock
23392d8e95SPeter Maydell      [...]
24392d8e95SPeter Maydell      (QEMU)
25392d8e95SPeter Maydell
26392d8e95SPeter Maydell(3) Find out which CPU types could be plugged, and into which sockets::
27392d8e95SPeter Maydell
28392d8e95SPeter Maydell      (QEMU) query-hotpluggable-cpus
29392d8e95SPeter Maydell      {
30392d8e95SPeter Maydell          "execute": "query-hotpluggable-cpus",
31392d8e95SPeter Maydell          "arguments": {}
32392d8e95SPeter Maydell      }
33392d8e95SPeter Maydell      {
34392d8e95SPeter Maydell          "return": [
35392d8e95SPeter Maydell              {
36392d8e95SPeter Maydell                  "props": {
37*bda8c24cSPeter Maydell                      "core-id": 1,
38*bda8c24cSPeter Maydell                      "socket-id": 0,
39392d8e95SPeter Maydell                      "thread-id": 0
40*bda8c24cSPeter Maydell                  },
41*bda8c24cSPeter Maydell                  "type": "IvyBridge-IBRS-x86_64-cpu",
42*bda8c24cSPeter Maydell                  "vcpus-count": 1
43392d8e95SPeter Maydell              },
44392d8e95SPeter Maydell              {
45*bda8c24cSPeter Maydell                  "props": {
46*bda8c24cSPeter Maydell                      "core-id": 0,
47*bda8c24cSPeter Maydell                      "socket-id": 0,
48*bda8c24cSPeter Maydell                      "thread-id": 0
49*bda8c24cSPeter Maydell                  },
50392d8e95SPeter Maydell                  "qom-path": "/machine/unattached/device[0]",
51392d8e95SPeter Maydell                  "type": "IvyBridge-IBRS-x86_64-cpu",
52*bda8c24cSPeter Maydell                  "vcpus-count": 1
53392d8e95SPeter Maydell              }
54392d8e95SPeter Maydell          ]
55392d8e95SPeter Maydell      }
56392d8e95SPeter Maydell      (QEMU)
57392d8e95SPeter Maydell
58392d8e95SPeter Maydell(4) The ``query-hotpluggable-cpus`` command returns an object for CPUs
59392d8e95SPeter Maydell    that are present (containing a "qom-path" member) or which may be
60392d8e95SPeter Maydell    hot-plugged (no "qom-path" member).  From its output in step (3), we
61*bda8c24cSPeter Maydell    can see that ``IvyBridge-IBRS-x86_64-cpu`` is present in socket 0 core 0,
62*bda8c24cSPeter Maydell    while hot-plugging a CPU into socket 0 core 1 requires passing the listed
63392d8e95SPeter Maydell    properties to QMP ``device_add``::
64392d8e95SPeter Maydell
65*bda8c24cSPeter Maydell      (QEMU) device_add id=cpu-2 driver=IvyBridge-IBRS-x86_64-cpu socket-id=0 core-id=1 thread-id=0
66392d8e95SPeter Maydell      {
67392d8e95SPeter Maydell          "execute": "device_add",
68392d8e95SPeter Maydell          "arguments": {
69*bda8c24cSPeter Maydell              "core-id": 1,
70392d8e95SPeter Maydell              "driver": "IvyBridge-IBRS-x86_64-cpu",
71392d8e95SPeter Maydell              "id": "cpu-2",
72*bda8c24cSPeter Maydell              "socket-id": 0,
73392d8e95SPeter Maydell              "thread-id": 0
74392d8e95SPeter Maydell          }
75392d8e95SPeter Maydell      }
76392d8e95SPeter Maydell      {
77392d8e95SPeter Maydell          "return": {}
78392d8e95SPeter Maydell      }
79392d8e95SPeter Maydell      (QEMU)
80392d8e95SPeter Maydell
816df743dcSPeter Maydell(5) Optionally, run QMP ``query-cpus-fast`` for some details about the
82392d8e95SPeter Maydell    vCPUs::
83392d8e95SPeter Maydell
84392d8e95SPeter Maydell      (QEMU) query-cpus-fast
85392d8e95SPeter Maydell      {
86392d8e95SPeter Maydell          "arguments": {}
87*bda8c24cSPeter Maydell          "execute": "query-cpus-fast",
88392d8e95SPeter Maydell      }
89392d8e95SPeter Maydell      {
90392d8e95SPeter Maydell          "return": [
91392d8e95SPeter Maydell              {
92392d8e95SPeter Maydell                  "cpu-index": 0,
93392d8e95SPeter Maydell                  "props": {
94392d8e95SPeter Maydell                      "core-id": 0,
95*bda8c24cSPeter Maydell                      "socket-id": 0,
96392d8e95SPeter Maydell                      "thread-id": 0
97392d8e95SPeter Maydell                  },
98*bda8c24cSPeter Maydell                  "qom-path": "/machine/unattached/device[0]",
99*bda8c24cSPeter Maydell                  "target": "x86_64",
100*bda8c24cSPeter Maydell                  "thread-id": 28957
101392d8e95SPeter Maydell              },
102392d8e95SPeter Maydell              {
103392d8e95SPeter Maydell                  "cpu-index": 1,
104392d8e95SPeter Maydell                  "props": {
105*bda8c24cSPeter Maydell                      "core-id": 1,
106*bda8c24cSPeter Maydell                      "socket-id": 0,
107392d8e95SPeter Maydell                      "thread-id": 0
108392d8e95SPeter Maydell                  },
109*bda8c24cSPeter Maydell                  "qom-path": "/machine/peripheral/cpu-2",
110*bda8c24cSPeter Maydell                  "target": "x86_64",
111*bda8c24cSPeter Maydell                  "thread-id": 29095
112392d8e95SPeter Maydell              }
113392d8e95SPeter Maydell          ]
114392d8e95SPeter Maydell      }
115392d8e95SPeter Maydell      (QEMU)
116392d8e95SPeter Maydell
117392d8e95SPeter MaydellvCPU hot-unplug
118392d8e95SPeter Maydell---------------
119392d8e95SPeter Maydell
120392d8e95SPeter MaydellFrom the 'qmp-shell', invoke the QMP ``device_del`` command::
121392d8e95SPeter Maydell
122392d8e95SPeter Maydell      (QEMU) device_del id=cpu-2
123392d8e95SPeter Maydell      {
124392d8e95SPeter Maydell          "arguments": {
125392d8e95SPeter Maydell              "id": "cpu-2"
126392d8e95SPeter Maydell          }
127*bda8c24cSPeter Maydell          "execute": "device_del",
128392d8e95SPeter Maydell      }
129392d8e95SPeter Maydell      {
130392d8e95SPeter Maydell          "return": {}
131392d8e95SPeter Maydell      }
132392d8e95SPeter Maydell      (QEMU)
133392d8e95SPeter Maydell
134392d8e95SPeter Maydell.. note::
135392d8e95SPeter Maydell    vCPU hot-unplug requires guest cooperation; so the ``device_del``
136392d8e95SPeter Maydell    command above does not guarantee vCPU removal -- it's a "request to
137392d8e95SPeter Maydell    unplug".  At this point, the guest will get a System Control
138392d8e95SPeter Maydell    Interrupt (SCI) and calls the ACPI handler for the affected vCPU
139392d8e95SPeter Maydell    device.  Then the guest kernel will bring the vCPU offline and tell
140392d8e95SPeter Maydell    QEMU to unplug it.
141