]> git.proxmox.com Git - mirror_qemu.git/blob - hw/s390x/ipl.c
s390x/ipl: drop reipl parameters on resets
[mirror_qemu.git] / hw / s390x / ipl.c
1 /*
2 * bootloader support
3 *
4 * Copyright IBM, Corp. 2012
5 *
6 * Authors:
7 * Christian Borntraeger <borntraeger@de.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or (at your
10 * option) any later version. See the COPYING file in the top-level directory.
11 *
12 */
13
14 #include "sysemu/sysemu.h"
15 #include "cpu.h"
16 #include "elf.h"
17 #include "hw/loader.h"
18 #include "hw/sysbus.h"
19 #include "hw/s390x/virtio-ccw.h"
20 #include "hw/s390x/css.h"
21 #include "ipl.h"
22
23 #define KERN_IMAGE_START 0x010000UL
24 #define KERN_PARM_AREA 0x010480UL
25 #define INITRD_START 0x800000UL
26 #define INITRD_PARM_START 0x010408UL
27 #define INITRD_PARM_SIZE 0x010410UL
28 #define PARMFILE_START 0x001000UL
29 #define ZIPL_IMAGE_START 0x009000UL
30 #define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64)
31
32 #define TYPE_S390_IPL "s390-ipl"
33 #define S390_IPL(obj) \
34 OBJECT_CHECK(S390IPLState, (obj), TYPE_S390_IPL)
35 #if 0
36 #define S390_IPL_CLASS(klass) \
37 OBJECT_CLASS_CHECK(S390IPLState, (klass), TYPE_S390_IPL)
38 #define S390_IPL_GET_CLASS(obj) \
39 OBJECT_GET_CLASS(S390IPLState, (obj), TYPE_S390_IPL)
40 #endif
41
42 typedef struct S390IPLClass {
43 /*< private >*/
44 SysBusDeviceClass parent_class;
45 /*< public >*/
46
47 void (*parent_reset) (SysBusDevice *dev);
48 } S390IPLClass;
49
50 typedef struct S390IPLState {
51 /*< private >*/
52 SysBusDevice parent_obj;
53 uint64_t start_addr;
54 uint64_t bios_start_addr;
55 bool enforce_bios;
56 IplParameterBlock iplb;
57 bool iplb_valid;
58 bool reipl_requested;
59
60 /*< public >*/
61 char *kernel;
62 char *initrd;
63 char *cmdline;
64 char *firmware;
65 uint8_t cssid;
66 uint8_t ssid;
67 uint16_t devno;
68 } S390IPLState;
69
70
71 static int s390_ipl_init(SysBusDevice *dev)
72 {
73 S390IPLState *ipl = S390_IPL(dev);
74 uint64_t pentry = KERN_IMAGE_START;
75 int kernel_size;
76
77 int bios_size;
78 char *bios_filename;
79
80 /*
81 * Always load the bios if it was enforced,
82 * even if an external kernel has been defined.
83 */
84 if (!ipl->kernel || ipl->enforce_bios) {
85 if (bios_name == NULL) {
86 bios_name = ipl->firmware;
87 }
88
89 bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
90 if (bios_filename == NULL) {
91 hw_error("could not find stage1 bootloader\n");
92 }
93
94 bios_size = load_elf(bios_filename, NULL, NULL, &ipl->bios_start_addr,
95 NULL, NULL, 1, ELF_MACHINE, 0);
96 if (bios_size < 0) {
97 bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START,
98 4096);
99 ipl->bios_start_addr = ZIPL_IMAGE_START;
100 if (bios_size > 4096) {
101 hw_error("stage1 bootloader is > 4k\n");
102 }
103 }
104 g_free(bios_filename);
105
106 if (bios_size == -1) {
107 hw_error("could not load bootloader '%s'\n", bios_name);
108 }
109
110 /* default boot target is the bios */
111 ipl->start_addr = ipl->bios_start_addr;
112 }
113
114 if (ipl->kernel) {
115 kernel_size = load_elf(ipl->kernel, NULL, NULL, &pentry, NULL,
116 NULL, 1, ELF_MACHINE, 0);
117 if (kernel_size < 0) {
118 kernel_size = load_image_targphys(ipl->kernel, 0, ram_size);
119 }
120 if (kernel_size < 0) {
121 fprintf(stderr, "could not load kernel '%s'\n", ipl->kernel);
122 return -1;
123 }
124 /*
125 * Is it a Linux kernel (starting at 0x10000)? If yes, we fill in the
126 * kernel parameters here as well. Note: For old kernels (up to 3.2)
127 * we can not rely on the ELF entry point - it was 0x800 (the SALIPL
128 * loader) and it won't work. For this case we force it to 0x10000, too.
129 */
130 if (pentry == KERN_IMAGE_START || pentry == 0x800) {
131 ipl->start_addr = KERN_IMAGE_START;
132 /* Overwrite parameters in the kernel image, which are "rom" */
133 strcpy(rom_ptr(KERN_PARM_AREA), ipl->cmdline);
134 } else {
135 ipl->start_addr = pentry;
136 }
137
138 if (ipl->initrd) {
139 ram_addr_t initrd_offset;
140 int initrd_size;
141
142 initrd_offset = INITRD_START;
143 while (kernel_size + 0x100000 > initrd_offset) {
144 initrd_offset += 0x100000;
145 }
146 initrd_size = load_image_targphys(ipl->initrd, initrd_offset,
147 ram_size - initrd_offset);
148 if (initrd_size == -1) {
149 fprintf(stderr, "qemu: could not load initrd '%s'\n",
150 ipl->initrd);
151 exit(1);
152 }
153
154 /*
155 * we have to overwrite values in the kernel image,
156 * which are "rom"
157 */
158 stq_p(rom_ptr(INITRD_PARM_START), initrd_offset);
159 stq_p(rom_ptr(INITRD_PARM_SIZE), initrd_size);
160 }
161 }
162 return 0;
163 }
164
165 static Property s390_ipl_properties[] = {
166 DEFINE_PROP_STRING("kernel", S390IPLState, kernel),
167 DEFINE_PROP_STRING("initrd", S390IPLState, initrd),
168 DEFINE_PROP_STRING("cmdline", S390IPLState, cmdline),
169 DEFINE_PROP_STRING("firmware", S390IPLState, firmware),
170 DEFINE_PROP_BOOL("enforce_bios", S390IPLState, enforce_bios, false),
171 DEFINE_PROP_END_OF_LIST(),
172 };
173
174 /*
175 * In addition to updating the iplstate, this function returns:
176 * - 0 if system was ipled with external kernel
177 * - -1 if no valid boot device was found
178 * - ccw id of the boot device otherwise
179 */
180 static uint64_t s390_update_iplstate(CPUS390XState *env, S390IPLState *ipl)
181 {
182 DeviceState *dev_st;
183
184 if (ipl->iplb_valid) {
185 ipl->cssid = 0;
186 ipl->ssid = 0;
187 ipl->devno = ipl->iplb.devno;
188 goto out;
189 }
190
191 if (ipl->kernel) {
192 return 0;
193 }
194
195 dev_st = get_boot_device(0);
196 if (dev_st) {
197 VirtioCcwDevice *ccw_dev = (VirtioCcwDevice *) object_dynamic_cast(
198 OBJECT(qdev_get_parent_bus(dev_st)->parent),
199 TYPE_VIRTIO_CCW_DEVICE);
200 if (ccw_dev) {
201 ipl->cssid = ccw_dev->sch->cssid;
202 ipl->ssid = ccw_dev->sch->ssid;
203 ipl->devno = ccw_dev->sch->devno;
204 goto out;
205 }
206 }
207
208 return -1;
209 out:
210 return ipl->cssid << 24 | ipl->ssid << 16 | ipl->devno;
211 }
212
213 int s390_ipl_update_diag308(IplParameterBlock *iplb)
214 {
215 S390IPLState *ipl;
216
217 ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
218 if (ipl) {
219 ipl->iplb = *iplb;
220 ipl->iplb_valid = true;
221 return 0;
222 }
223 return -1;
224 }
225
226 IplParameterBlock *s390_ipl_get_iplb(void)
227 {
228 S390IPLState *ipl;
229
230 ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
231 if (!ipl || !ipl->iplb_valid) {
232 return NULL;
233 }
234 return &ipl->iplb;
235 }
236
237 void s390_reipl_request(void)
238 {
239 S390IPLState *ipl;
240
241 ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
242 ipl->reipl_requested = true;
243 qemu_system_reset_request();
244 }
245
246 static void s390_ipl_reset(DeviceState *dev)
247 {
248 S390IPLState *ipl = S390_IPL(dev);
249 S390CPU *cpu = S390_CPU(qemu_get_cpu(0));
250 CPUS390XState *env = &cpu->env;
251
252 env->psw.addr = ipl->start_addr;
253 env->psw.mask = IPL_PSW_MASK;
254
255 if (!ipl->reipl_requested) {
256 ipl->iplb_valid = false;
257 }
258 ipl->reipl_requested = false;
259
260 if (!ipl->kernel || ipl->iplb_valid) {
261 env->psw.addr = ipl->bios_start_addr;
262 env->regs[7] = s390_update_iplstate(env, ipl);
263 }
264
265 s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
266 }
267
268 static void s390_ipl_class_init(ObjectClass *klass, void *data)
269 {
270 DeviceClass *dc = DEVICE_CLASS(klass);
271 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
272
273 k->init = s390_ipl_init;
274 dc->props = s390_ipl_properties;
275 dc->reset = s390_ipl_reset;
276 }
277
278 static const TypeInfo s390_ipl_info = {
279 .class_init = s390_ipl_class_init,
280 .parent = TYPE_SYS_BUS_DEVICE,
281 .name = "s390-ipl",
282 .instance_size = sizeof(S390IPLState),
283 };
284
285 static void s390_ipl_register_types(void)
286 {
287 type_register_static(&s390_ipl_info);
288 }
289
290 type_init(s390_ipl_register_types)