1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25#include "qapi/error.h"
26#include "hw/hw.h"
27#include "qapi/qmp/qerror.h"
28#include "qemu/error-report.h"
29#include "sysemu/block-backend.h"
30#include "sysemu/blockdev.h"
31#include "sysemu/sysemu.h"
32#include "net/net.h"
33#include "hw/boards.h"
34#include "hw/loader.h"
35#include "hw/virtio/virtio.h"
36#include "sysemu/kvm.h"
37#include "exec/address-spaces.h"
38#include "sysemu/qtest.h"
39
40#include "hw/s390x/sclp.h"
41#include "hw/s390x/s390_flic.h"
42#include "hw/s390x/s390-virtio.h"
43#include "hw/s390x/storage-keys.h"
44#include "hw/s390x/ipl.h"
45#include "cpu.h"
46
47
48
49#ifdef DEBUG_S390
50#define DPRINTF(fmt, ...) \
51 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
52#else
53#define DPRINTF(fmt, ...) \
54 do { } while (0)
55#endif
56
57#define MAX_BLK_DEVS 10
58
59#define S390_TOD_CLOCK_VALUE_MISSING 0x00
60#define S390_TOD_CLOCK_VALUE_PRESENT 0x01
61
62static S390CPU **cpu_states;
63
64S390CPU *s390_cpu_addr2state(uint16_t cpu_addr)
65{
66 if (cpu_addr >= max_cpus) {
67 return NULL;
68 }
69
70
71 return cpu_states[cpu_addr];
72}
73
74void s390_init_ipl_dev(const char *kernel_filename,
75 const char *kernel_cmdline,
76 const char *initrd_filename,
77 const char *firmware,
78 bool enforce_bios)
79{
80 Object *new = object_new(TYPE_S390_IPL);
81 DeviceState *dev = DEVICE(new);
82
83 if (kernel_filename) {
84 qdev_prop_set_string(dev, "kernel", kernel_filename);
85 }
86 if (initrd_filename) {
87 qdev_prop_set_string(dev, "initrd", initrd_filename);
88 }
89 qdev_prop_set_string(dev, "cmdline", kernel_cmdline);
90 qdev_prop_set_string(dev, "firmware", firmware);
91 qdev_prop_set_bit(dev, "enforce_bios", enforce_bios);
92 object_property_add_child(qdev_get_machine(), TYPE_S390_IPL,
93 new, NULL);
94 object_unref(new);
95 qdev_init_nofail(dev);
96}
97
98void s390_init_cpus(MachineState *machine)
99{
100 int i;
101 gchar *name;
102
103 if (machine->cpu_model == NULL) {
104 machine->cpu_model = "host";
105 }
106
107 cpu_states = g_new0(S390CPU *, max_cpus);
108
109 for (i = 0; i < max_cpus; i++) {
110 name = g_strdup_printf("cpu[%i]", i);
111 object_property_add_link(OBJECT(machine), name, TYPE_S390_CPU,
112 (Object **) &cpu_states[i],
113 object_property_allow_set_link,
114 OBJ_PROP_LINK_UNREF_ON_RELEASE,
115 &error_abort);
116 g_free(name);
117 }
118
119 for (i = 0; i < smp_cpus; i++) {
120 s390x_new_cpu(machine->cpu_model, i, &error_fatal);
121 }
122}
123
124
125void s390_create_virtio_net(BusState *bus, const char *name)
126{
127 int i;
128
129 for (i = 0; i < nb_nics; i++) {
130 NICInfo *nd = &nd_table[i];
131 DeviceState *dev;
132
133 if (!nd->model) {
134 nd->model = g_strdup("virtio");
135 }
136
137 qemu_check_nic_model(nd, "virtio");
138
139 dev = qdev_create(bus, name);
140 qdev_set_nic_properties(dev, nd);
141 qdev_init_nofail(dev);
142 }
143}
144
145void gtod_save(QEMUFile *f, void *opaque)
146{
147 uint64_t tod_low;
148 uint8_t tod_high;
149 int r;
150
151 r = s390_get_clock(&tod_high, &tod_low);
152 if (r) {
153 fprintf(stderr, "WARNING: Unable to get guest clock for migration. "
154 "Error code %d. Guest clock will not be migrated "
155 "which could cause the guest to hang.\n", r);
156 qemu_put_byte(f, S390_TOD_CLOCK_VALUE_MISSING);
157 return;
158 }
159
160 qemu_put_byte(f, S390_TOD_CLOCK_VALUE_PRESENT);
161 qemu_put_byte(f, tod_high);
162 qemu_put_be64(f, tod_low);
163}
164
165int gtod_load(QEMUFile *f, void *opaque, int version_id)
166{
167 uint64_t tod_low;
168 uint8_t tod_high;
169 int r;
170
171 if (qemu_get_byte(f) == S390_TOD_CLOCK_VALUE_MISSING) {
172 fprintf(stderr, "WARNING: Guest clock was not migrated. This could "
173 "cause the guest to hang.\n");
174 return 0;
175 }
176
177 tod_high = qemu_get_byte(f);
178 tod_low = qemu_get_be64(f);
179
180 r = s390_set_clock(&tod_high, &tod_low);
181 if (r) {
182 fprintf(stderr, "WARNING: Unable to set guest clock value. "
183 "s390_get_clock returned error %d. This could cause "
184 "the guest to hang.\n", r);
185 }
186
187 return 0;
188}
189
190void s390_nmi(NMIState *n, int cpu_index, Error **errp)
191{
192 CPUState *cs = qemu_get_cpu(cpu_index);
193
194 if (s390_cpu_restart(S390_CPU(cs))) {
195 error_setg(errp, QERR_UNSUPPORTED);
196 }
197}
198
199void s390_machine_reset(void)
200{
201 S390CPU *ipl_cpu = S390_CPU(qemu_get_cpu(0));
202
203 qemu_devices_reset();
204 s390_cmma_reset();
205 s390_crypto_reset();
206
207
208 s390_ipl_prepare_cpu(ipl_cpu);
209 s390_cpu_set_state(CPU_STATE_OPERATING, ipl_cpu);
210}
211