pc.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /*
  2. * QEMU PC System Emulator
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "hw.h"
  25. #include "pc.h"
  26. #include "serial.h"
  27. #include "apic.h"
  28. #include "fdc.h"
  29. #include "ide.h"
  30. #include "pci/pci.h"
  31. #include "monitor/monitor.h"
  32. #include "fw_cfg.h"
  33. #include "hpet_emul.h"
  34. #include "smbios.h"
  35. #include "loader.h"
  36. #include "elf.h"
  37. #include "multiboot.h"
  38. #include "mc146818rtc.h"
  39. #include "i8254.h"
  40. #include "pcspk.h"
  41. #include "pci/msi.h"
  42. #include "sysbus.h"
  43. #include "sysemu/sysemu.h"
  44. #include "sysemu/kvm.h"
  45. #include "kvm_i386.h"
  46. #include "xen.h"
  47. #include "sysemu/blockdev.h"
  48. #include "hw/block-common.h"
  49. #include "ui/qemu-spice.h"
  50. #include "exec/memory.h"
  51. #include "exec/address-spaces.h"
  52. #include "sysemu/arch_init.h"
  53. #include "qemu/bitmap.h"
  54. /* debug PC/ISA interrupts */
  55. //#define DEBUG_IRQ
  56. #ifdef DEBUG_IRQ
  57. #define DPRINTF(fmt, ...) \
  58. do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
  59. #else
  60. #define DPRINTF(fmt, ...)
  61. #endif
  62. /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */
  63. #define ACPI_DATA_SIZE 0x10000
  64. #define BIOS_CFG_IOPORT 0x510
  65. #define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
  66. #define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1)
  67. #define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2)
  68. #define FW_CFG_E820_TABLE (FW_CFG_ARCH_LOCAL + 3)
  69. #define FW_CFG_HPET (FW_CFG_ARCH_LOCAL + 4)
  70. #define E820_NR_ENTRIES 16
  71. struct e820_entry {
  72. uint64_t address;
  73. uint64_t length;
  74. uint32_t type;
  75. } QEMU_PACKED __attribute((__aligned__(4)));
  76. struct e820_table {
  77. uint32_t count;
  78. struct e820_entry entry[E820_NR_ENTRIES];
  79. } QEMU_PACKED __attribute((__aligned__(4)));
  80. static struct e820_table e820_table;
  81. struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
  82. void gsi_handler(void *opaque, int n, int level)
  83. {
  84. GSIState *s = opaque;
  85. DPRINTF("pc: %s GSI %d\n", level ? "raising" : "lowering", n);
  86. if (n < ISA_NUM_IRQS) {
  87. qemu_set_irq(s->i8259_irq[n], level);
  88. }
  89. qemu_set_irq(s->ioapic_irq[n], level);
  90. }
  91. static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
  92. unsigned size)
  93. {
  94. }
  95. static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
  96. {
  97. return 0xffffffffffffffffULL;
  98. }
  99. /* MSDOS compatibility mode FPU exception support */
  100. static qemu_irq ferr_irq;
  101. void pc_register_ferr_irq(qemu_irq irq)
  102. {
  103. ferr_irq = irq;
  104. }
  105. /* XXX: add IGNNE support */
  106. void cpu_set_ferr(CPUX86State *s)
  107. {
  108. qemu_irq_raise(ferr_irq);
  109. }
  110. static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
  111. unsigned size)
  112. {
  113. qemu_irq_lower(ferr_irq);
  114. }
  115. static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
  116. {
  117. return 0xffffffffffffffffULL;
  118. }
  119. /* TSC handling */
  120. uint64_t cpu_get_tsc(CPUX86State *env)
  121. {
  122. return cpu_get_ticks();
  123. }
  124. /* SMM support */
  125. static cpu_set_smm_t smm_set;
  126. static void *smm_arg;
  127. void cpu_smm_register(cpu_set_smm_t callback, void *arg)
  128. {
  129. assert(smm_set == NULL);
  130. assert(smm_arg == NULL);
  131. smm_set = callback;
  132. smm_arg = arg;
  133. }
  134. void cpu_smm_update(CPUX86State *env)
  135. {
  136. if (smm_set && smm_arg && env == first_cpu)
  137. smm_set(!!(env->hflags & HF_SMM_MASK), smm_arg);
  138. }
  139. /* IRQ handling */
  140. int cpu_get_pic_interrupt(CPUX86State *env)
  141. {
  142. int intno;
  143. intno = apic_get_interrupt(env->apic_state);
  144. if (intno >= 0) {
  145. return intno;
  146. }
  147. /* read the irq from the PIC */
  148. if (!apic_accept_pic_intr(env->apic_state)) {
  149. return -1;
  150. }
  151. intno = pic_read_irq(isa_pic);
  152. return intno;
  153. }
  154. static void pic_irq_request(void *opaque, int irq, int level)
  155. {
  156. CPUX86State *env = first_cpu;
  157. DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq);
  158. if (env->apic_state) {
  159. while (env) {
  160. if (apic_accept_pic_intr(env->apic_state)) {
  161. apic_deliver_pic_intr(env->apic_state, level);
  162. }
  163. env = env->next_cpu;
  164. }
  165. } else {
  166. if (level)
  167. cpu_interrupt(env, CPU_INTERRUPT_HARD);
  168. else
  169. cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
  170. }
  171. }
  172. /* PC cmos mappings */
  173. #define REG_EQUIPMENT_BYTE 0x14
  174. static int cmos_get_fd_drive_type(FDriveType fd0)
  175. {
  176. int val;
  177. switch (fd0) {
  178. case FDRIVE_DRV_144:
  179. /* 1.44 Mb 3"5 drive */
  180. val = 4;
  181. break;
  182. case FDRIVE_DRV_288:
  183. /* 2.88 Mb 3"5 drive */
  184. val = 5;
  185. break;
  186. case FDRIVE_DRV_120:
  187. /* 1.2 Mb 5"5 drive */
  188. val = 2;
  189. break;
  190. case FDRIVE_DRV_NONE:
  191. default:
  192. val = 0;
  193. break;
  194. }
  195. return val;
  196. }
  197. static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
  198. int16_t cylinders, int8_t heads, int8_t sectors)
  199. {
  200. rtc_set_memory(s, type_ofs, 47);
  201. rtc_set_memory(s, info_ofs, cylinders);
  202. rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
  203. rtc_set_memory(s, info_ofs + 2, heads);
  204. rtc_set_memory(s, info_ofs + 3, 0xff);
  205. rtc_set_memory(s, info_ofs + 4, 0xff);
  206. rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
  207. rtc_set_memory(s, info_ofs + 6, cylinders);
  208. rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
  209. rtc_set_memory(s, info_ofs + 8, sectors);
  210. }
  211. /* convert boot_device letter to something recognizable by the bios */
  212. static int boot_device2nibble(char boot_device)
  213. {
  214. switch(boot_device) {
  215. case 'a':
  216. case 'b':
  217. return 0x01; /* floppy boot */
  218. case 'c':
  219. return 0x02; /* hard drive boot */
  220. case 'd':
  221. return 0x03; /* CD-ROM boot */
  222. case 'n':
  223. return 0x04; /* Network boot */
  224. }
  225. return 0;
  226. }
  227. static int set_boot_dev(ISADevice *s, const char *boot_device, int fd_bootchk)
  228. {
  229. #define PC_MAX_BOOT_DEVICES 3
  230. int nbds, bds[3] = { 0, };
  231. int i;
  232. nbds = strlen(boot_device);
  233. if (nbds > PC_MAX_BOOT_DEVICES) {
  234. error_report("Too many boot devices for PC");
  235. return(1);
  236. }
  237. for (i = 0; i < nbds; i++) {
  238. bds[i] = boot_device2nibble(boot_device[i]);
  239. if (bds[i] == 0) {
  240. error_report("Invalid boot device for PC: '%c'",
  241. boot_device[i]);
  242. return(1);
  243. }
  244. }
  245. rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
  246. rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
  247. return(0);
  248. }
  249. static int pc_boot_set(void *opaque, const char *boot_device)
  250. {
  251. return set_boot_dev(opaque, boot_device, 0);
  252. }
  253. typedef struct pc_cmos_init_late_arg {
  254. ISADevice *rtc_state;
  255. BusState *idebus[2];
  256. } pc_cmos_init_late_arg;
  257. static void pc_cmos_init_late(void *opaque)
  258. {
  259. pc_cmos_init_late_arg *arg = opaque;
  260. ISADevice *s = arg->rtc_state;
  261. int16_t cylinders;
  262. int8_t heads, sectors;
  263. int val;
  264. int i, trans;
  265. val = 0;
  266. if (ide_get_geometry(arg->idebus[0], 0,
  267. &cylinders, &heads, &sectors) >= 0) {
  268. cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
  269. val |= 0xf0;
  270. }
  271. if (ide_get_geometry(arg->idebus[0], 1,
  272. &cylinders, &heads, &sectors) >= 0) {
  273. cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
  274. val |= 0x0f;
  275. }
  276. rtc_set_memory(s, 0x12, val);
  277. val = 0;
  278. for (i = 0; i < 4; i++) {
  279. /* NOTE: ide_get_geometry() returns the physical
  280. geometry. It is always such that: 1 <= sects <= 63, 1
  281. <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
  282. geometry can be different if a translation is done. */
  283. if (ide_get_geometry(arg->idebus[i / 2], i % 2,
  284. &cylinders, &heads, &sectors) >= 0) {
  285. trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
  286. assert((trans & ~3) == 0);
  287. val |= trans << (i * 2);
  288. }
  289. }
  290. rtc_set_memory(s, 0x39, val);
  291. qemu_unregister_reset(pc_cmos_init_late, opaque);
  292. }
  293. void pc_cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
  294. const char *boot_device,
  295. ISADevice *floppy, BusState *idebus0, BusState *idebus1,
  296. ISADevice *s)
  297. {
  298. int val, nb, i;
  299. FDriveType fd_type[2] = { FDRIVE_DRV_NONE, FDRIVE_DRV_NONE };
  300. static pc_cmos_init_late_arg arg;
  301. /* various important CMOS locations needed by PC/Bochs bios */
  302. /* memory size */
  303. /* base memory (first MiB) */
  304. val = MIN(ram_size / 1024, 640);
  305. rtc_set_memory(s, 0x15, val);
  306. rtc_set_memory(s, 0x16, val >> 8);
  307. /* extended memory (next 64MiB) */
  308. if (ram_size > 1024 * 1024) {
  309. val = (ram_size - 1024 * 1024) / 1024;
  310. } else {
  311. val = 0;
  312. }
  313. if (val > 65535)
  314. val = 65535;
  315. rtc_set_memory(s, 0x17, val);
  316. rtc_set_memory(s, 0x18, val >> 8);
  317. rtc_set_memory(s, 0x30, val);
  318. rtc_set_memory(s, 0x31, val >> 8);
  319. /* memory between 16MiB and 4GiB */
  320. if (ram_size > 16 * 1024 * 1024) {
  321. val = (ram_size - 16 * 1024 * 1024) / 65536;
  322. } else {
  323. val = 0;
  324. }
  325. if (val > 65535)
  326. val = 65535;
  327. rtc_set_memory(s, 0x34, val);
  328. rtc_set_memory(s, 0x35, val >> 8);
  329. /* memory above 4GiB */
  330. val = above_4g_mem_size / 65536;
  331. rtc_set_memory(s, 0x5b, val);
  332. rtc_set_memory(s, 0x5c, val >> 8);
  333. rtc_set_memory(s, 0x5d, val >> 16);
  334. /* set the number of CPU */
  335. rtc_set_memory(s, 0x5f, smp_cpus - 1);
  336. /* set boot devices, and disable floppy signature check if requested */
  337. if (set_boot_dev(s, boot_device, fd_bootchk)) {
  338. exit(1);
  339. }
  340. /* floppy type */
  341. if (floppy) {
  342. for (i = 0; i < 2; i++) {
  343. fd_type[i] = isa_fdc_get_drive_type(floppy, i);
  344. }
  345. }
  346. val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
  347. cmos_get_fd_drive_type(fd_type[1]);
  348. rtc_set_memory(s, 0x10, val);
  349. val = 0;
  350. nb = 0;
  351. if (fd_type[0] < FDRIVE_DRV_NONE) {
  352. nb++;
  353. }
  354. if (fd_type[1] < FDRIVE_DRV_NONE) {
  355. nb++;
  356. }
  357. switch (nb) {
  358. case 0:
  359. break;
  360. case 1:
  361. val |= 0x01; /* 1 drive, ready for boot */
  362. break;
  363. case 2:
  364. val |= 0x41; /* 2 drives, ready for boot */
  365. break;
  366. }
  367. val |= 0x02; /* FPU is there */
  368. val |= 0x04; /* PS/2 mouse installed */
  369. rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
  370. /* hard drives */
  371. arg.rtc_state = s;
  372. arg.idebus[0] = idebus0;
  373. arg.idebus[1] = idebus1;
  374. qemu_register_reset(pc_cmos_init_late, &arg);
  375. }
  376. /* port 92 stuff: could be split off */
  377. typedef struct Port92State {
  378. ISADevice dev;
  379. MemoryRegion io;
  380. uint8_t outport;
  381. qemu_irq *a20_out;
  382. } Port92State;
  383. static void port92_write(void *opaque, hwaddr addr, uint64_t val,
  384. unsigned size)
  385. {
  386. Port92State *s = opaque;
  387. DPRINTF("port92: write 0x%02x\n", val);
  388. s->outport = val;
  389. qemu_set_irq(*s->a20_out, (val >> 1) & 1);
  390. if (val & 1) {
  391. qemu_system_reset_request();
  392. }
  393. }
  394. static uint64_t port92_read(void *opaque, hwaddr addr,
  395. unsigned size)
  396. {
  397. Port92State *s = opaque;
  398. uint32_t ret;
  399. ret = s->outport;
  400. DPRINTF("port92: read 0x%02x\n", ret);
  401. return ret;
  402. }
  403. static void port92_init(ISADevice *dev, qemu_irq *a20_out)
  404. {
  405. Port92State *s = DO_UPCAST(Port92State, dev, dev);
  406. s->a20_out = a20_out;
  407. }
  408. static const VMStateDescription vmstate_port92_isa = {
  409. .name = "port92",
  410. .version_id = 1,
  411. .minimum_version_id = 1,
  412. .minimum_version_id_old = 1,
  413. .fields = (VMStateField []) {
  414. VMSTATE_UINT8(outport, Port92State),
  415. VMSTATE_END_OF_LIST()
  416. }
  417. };
  418. static void port92_reset(DeviceState *d)
  419. {
  420. Port92State *s = container_of(d, Port92State, dev.qdev);
  421. s->outport &= ~1;
  422. }
  423. static const MemoryRegionOps port92_ops = {
  424. .read = port92_read,
  425. .write = port92_write,
  426. .impl = {
  427. .min_access_size = 1,
  428. .max_access_size = 1,
  429. },
  430. .endianness = DEVICE_LITTLE_ENDIAN,
  431. };
  432. static int port92_initfn(ISADevice *dev)
  433. {
  434. Port92State *s = DO_UPCAST(Port92State, dev, dev);
  435. memory_region_init_io(&s->io, &port92_ops, s, "port92", 1);
  436. isa_register_ioport(dev, &s->io, 0x92);
  437. s->outport = 0;
  438. return 0;
  439. }
  440. static void port92_class_initfn(ObjectClass *klass, void *data)
  441. {
  442. DeviceClass *dc = DEVICE_CLASS(klass);
  443. ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
  444. ic->init = port92_initfn;
  445. dc->no_user = 1;
  446. dc->reset = port92_reset;
  447. dc->vmsd = &vmstate_port92_isa;
  448. }
  449. static const TypeInfo port92_info = {
  450. .name = "port92",
  451. .parent = TYPE_ISA_DEVICE,
  452. .instance_size = sizeof(Port92State),
  453. .class_init = port92_class_initfn,
  454. };
  455. static void port92_register_types(void)
  456. {
  457. type_register_static(&port92_info);
  458. }
  459. type_init(port92_register_types)
  460. static void handle_a20_line_change(void *opaque, int irq, int level)
  461. {
  462. X86CPU *cpu = opaque;
  463. /* XXX: send to all CPUs ? */
  464. /* XXX: add logic to handle multiple A20 line sources */
  465. x86_cpu_set_a20(cpu, level);
  466. }
  467. int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
  468. {
  469. int index = le32_to_cpu(e820_table.count);
  470. struct e820_entry *entry;
  471. if (index >= E820_NR_ENTRIES)
  472. return -EBUSY;
  473. entry = &e820_table.entry[index++];
  474. entry->address = cpu_to_le64(address);
  475. entry->length = cpu_to_le64(length);
  476. entry->type = cpu_to_le32(type);
  477. e820_table.count = cpu_to_le32(index);
  478. return index;
  479. }
  480. /* Calculates the limit to CPU APIC ID values
  481. *
  482. * This function returns the limit for the APIC ID value, so that all
  483. * CPU APIC IDs are < pc_apic_id_limit().
  484. *
  485. * This is used for FW_CFG_MAX_CPUS. See comments on bochs_bios_init().
  486. */
  487. static unsigned int pc_apic_id_limit(unsigned int max_cpus)
  488. {
  489. return x86_cpu_apic_id_from_index(max_cpus - 1) + 1;
  490. }
  491. static void *bochs_bios_init(void)
  492. {
  493. void *fw_cfg;
  494. uint8_t *smbios_table;
  495. size_t smbios_len;
  496. uint64_t *numa_fw_cfg;
  497. int i, j;
  498. unsigned int apic_id_limit = pc_apic_id_limit(max_cpus);
  499. fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
  500. /* FW_CFG_MAX_CPUS is a bit confusing/problematic on x86:
  501. *
  502. * SeaBIOS needs FW_CFG_MAX_CPUS for CPU hotplug, but the CPU hotplug
  503. * QEMU<->SeaBIOS interface is not based on the "CPU index", but on the APIC
  504. * ID of hotplugged CPUs[1]. This means that FW_CFG_MAX_CPUS is not the
  505. * "maximum number of CPUs", but the "limit to the APIC ID values SeaBIOS
  506. * may see".
  507. *
  508. * So, this means we must not use max_cpus, here, but the maximum possible
  509. * APIC ID value, plus one.
  510. *
  511. * [1] The only kind of "CPU identifier" used between SeaBIOS and QEMU is
  512. * the APIC ID, not the "CPU index"
  513. */
  514. fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)apic_id_limit);
  515. fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
  516. fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
  517. fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES,
  518. acpi_tables, acpi_tables_len);
  519. fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, kvm_allows_irq0_override());
  520. smbios_table = smbios_get_table(&smbios_len);
  521. if (smbios_table)
  522. fw_cfg_add_bytes(fw_cfg, FW_CFG_SMBIOS_ENTRIES,
  523. smbios_table, smbios_len);
  524. fw_cfg_add_bytes(fw_cfg, FW_CFG_E820_TABLE,
  525. &e820_table, sizeof(e820_table));
  526. fw_cfg_add_bytes(fw_cfg, FW_CFG_HPET, &hpet_cfg, sizeof(hpet_cfg));
  527. /* allocate memory for the NUMA channel: one (64bit) word for the number
  528. * of nodes, one word for each VCPU->node and one word for each node to
  529. * hold the amount of memory.
  530. */
  531. numa_fw_cfg = g_new0(uint64_t, 1 + apic_id_limit + nb_numa_nodes);
  532. numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
  533. for (i = 0; i < max_cpus; i++) {
  534. unsigned int apic_id = x86_cpu_apic_id_from_index(i);
  535. assert(apic_id < apic_id_limit);
  536. for (j = 0; j < nb_numa_nodes; j++) {
  537. if (test_bit(i, node_cpumask[j])) {
  538. numa_fw_cfg[apic_id + 1] = cpu_to_le64(j);
  539. break;
  540. }
  541. }
  542. }
  543. for (i = 0; i < nb_numa_nodes; i++) {
  544. numa_fw_cfg[apic_id_limit + 1 + i] = cpu_to_le64(node_mem[i]);
  545. }
  546. fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, numa_fw_cfg,
  547. (1 + apic_id_limit + nb_numa_nodes) *
  548. sizeof(*numa_fw_cfg));
  549. return fw_cfg;
  550. }
  551. static long get_file_size(FILE *f)
  552. {
  553. long where, size;
  554. /* XXX: on Unix systems, using fstat() probably makes more sense */
  555. where = ftell(f);
  556. fseek(f, 0, SEEK_END);
  557. size = ftell(f);
  558. fseek(f, where, SEEK_SET);
  559. return size;
  560. }
  561. static void load_linux(void *fw_cfg,
  562. const char *kernel_filename,
  563. const char *initrd_filename,
  564. const char *kernel_cmdline,
  565. hwaddr max_ram_size)
  566. {
  567. uint16_t protocol;
  568. int setup_size, kernel_size, initrd_size = 0, cmdline_size;
  569. uint32_t initrd_max;
  570. uint8_t header[8192], *setup, *kernel, *initrd_data;
  571. hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
  572. FILE *f;
  573. char *vmode;
  574. /* Align to 16 bytes as a paranoia measure */
  575. cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
  576. /* load the kernel header */
  577. f = fopen(kernel_filename, "rb");
  578. if (!f || !(kernel_size = get_file_size(f)) ||
  579. fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
  580. MIN(ARRAY_SIZE(header), kernel_size)) {
  581. fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
  582. kernel_filename, strerror(errno));
  583. exit(1);
  584. }
  585. /* kernel protocol version */
  586. #if 0
  587. fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
  588. #endif
  589. if (ldl_p(header+0x202) == 0x53726448)
  590. protocol = lduw_p(header+0x206);
  591. else {
  592. /* This looks like a multiboot kernel. If it is, let's stop
  593. treating it like a Linux kernel. */
  594. if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
  595. kernel_cmdline, kernel_size, header))
  596. return;
  597. protocol = 0;
  598. }
  599. if (protocol < 0x200 || !(header[0x211] & 0x01)) {
  600. /* Low kernel */
  601. real_addr = 0x90000;
  602. cmdline_addr = 0x9a000 - cmdline_size;
  603. prot_addr = 0x10000;
  604. } else if (protocol < 0x202) {
  605. /* High but ancient kernel */
  606. real_addr = 0x90000;
  607. cmdline_addr = 0x9a000 - cmdline_size;
  608. prot_addr = 0x100000;
  609. } else {
  610. /* High and recent kernel */
  611. real_addr = 0x10000;
  612. cmdline_addr = 0x20000;
  613. prot_addr = 0x100000;
  614. }
  615. #if 0
  616. fprintf(stderr,
  617. "qemu: real_addr = 0x" TARGET_FMT_plx "\n"
  618. "qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n"
  619. "qemu: prot_addr = 0x" TARGET_FMT_plx "\n",
  620. real_addr,
  621. cmdline_addr,
  622. prot_addr);
  623. #endif
  624. /* highest address for loading the initrd */
  625. if (protocol >= 0x203)
  626. initrd_max = ldl_p(header+0x22c);
  627. else
  628. initrd_max = 0x37ffffff;
  629. if (initrd_max >= max_ram_size-ACPI_DATA_SIZE)
  630. initrd_max = max_ram_size-ACPI_DATA_SIZE-1;
  631. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
  632. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline)+1);
  633. fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
  634. if (protocol >= 0x202) {
  635. stl_p(header+0x228, cmdline_addr);
  636. } else {
  637. stw_p(header+0x20, 0xA33F);
  638. stw_p(header+0x22, cmdline_addr-real_addr);
  639. }
  640. /* handle vga= parameter */
  641. vmode = strstr(kernel_cmdline, "vga=");
  642. if (vmode) {
  643. unsigned int video_mode;
  644. /* skip "vga=" */
  645. vmode += 4;
  646. if (!strncmp(vmode, "normal", 6)) {
  647. video_mode = 0xffff;
  648. } else if (!strncmp(vmode, "ext", 3)) {
  649. video_mode = 0xfffe;
  650. } else if (!strncmp(vmode, "ask", 3)) {
  651. video_mode = 0xfffd;
  652. } else {
  653. video_mode = strtol(vmode, NULL, 0);
  654. }
  655. stw_p(header+0x1fa, video_mode);
  656. }
  657. /* loader type */
  658. /* High nybble = B reserved for QEMU; low nybble is revision number.
  659. If this code is substantially changed, you may want to consider
  660. incrementing the revision. */
  661. if (protocol >= 0x200)
  662. header[0x210] = 0xB0;
  663. /* heap */
  664. if (protocol >= 0x201) {
  665. header[0x211] |= 0x80; /* CAN_USE_HEAP */
  666. stw_p(header+0x224, cmdline_addr-real_addr-0x200);
  667. }
  668. /* load initrd */
  669. if (initrd_filename) {
  670. if (protocol < 0x200) {
  671. fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
  672. exit(1);
  673. }
  674. initrd_size = get_image_size(initrd_filename);
  675. if (initrd_size < 0) {
  676. fprintf(stderr, "qemu: error reading initrd %s\n",
  677. initrd_filename);
  678. exit(1);
  679. }
  680. initrd_addr = (initrd_max-initrd_size) & ~4095;
  681. initrd_data = g_malloc(initrd_size);
  682. load_image(initrd_filename, initrd_data);
  683. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
  684. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
  685. fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
  686. stl_p(header+0x218, initrd_addr);
  687. stl_p(header+0x21c, initrd_size);
  688. }
  689. /* load kernel and setup */
  690. setup_size = header[0x1f1];
  691. if (setup_size == 0)
  692. setup_size = 4;
  693. setup_size = (setup_size+1)*512;
  694. kernel_size -= setup_size;
  695. setup = g_malloc(setup_size);
  696. kernel = g_malloc(kernel_size);
  697. fseek(f, 0, SEEK_SET);
  698. if (fread(setup, 1, setup_size, f) != setup_size) {
  699. fprintf(stderr, "fread() failed\n");
  700. exit(1);
  701. }
  702. if (fread(kernel, 1, kernel_size, f) != kernel_size) {
  703. fprintf(stderr, "fread() failed\n");
  704. exit(1);
  705. }
  706. fclose(f);
  707. memcpy(setup, header, MIN(sizeof(header), setup_size));
  708. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
  709. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
  710. fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
  711. fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
  712. fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
  713. fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
  714. option_rom[nb_option_roms].name = "linuxboot.bin";
  715. option_rom[nb_option_roms].bootindex = 0;
  716. nb_option_roms++;
  717. }
  718. #define NE2000_NB_MAX 6
  719. static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
  720. 0x280, 0x380 };
  721. static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
  722. static const int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
  723. static const int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
  724. void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
  725. {
  726. static int nb_ne2k = 0;
  727. if (nb_ne2k == NE2000_NB_MAX)
  728. return;
  729. isa_ne2000_init(bus, ne2000_io[nb_ne2k],
  730. ne2000_irq[nb_ne2k], nd);
  731. nb_ne2k++;
  732. }
  733. DeviceState *cpu_get_current_apic(void)
  734. {
  735. if (cpu_single_env) {
  736. return cpu_single_env->apic_state;
  737. } else {
  738. return NULL;
  739. }
  740. }
  741. void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
  742. {
  743. CPUX86State *s = opaque;
  744. if (level) {
  745. cpu_interrupt(s, CPU_INTERRUPT_SMI);
  746. }
  747. }
  748. void pc_cpus_init(const char *cpu_model)
  749. {
  750. int i;
  751. /* init CPUs */
  752. if (cpu_model == NULL) {
  753. #ifdef TARGET_X86_64
  754. cpu_model = "qemu64";
  755. #else
  756. cpu_model = "qemu32";
  757. #endif
  758. }
  759. for (i = 0; i < smp_cpus; i++) {
  760. if (!cpu_x86_init(cpu_model)) {
  761. fprintf(stderr, "Unable to find x86 CPU definition\n");
  762. exit(1);
  763. }
  764. }
  765. }
  766. void pc_acpi_init(const char *default_dsdt)
  767. {
  768. char *filename = NULL, *arg = NULL;
  769. if (acpi_tables != NULL) {
  770. /* manually set via -acpitable, leave it alone */
  771. return;
  772. }
  773. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, default_dsdt);
  774. if (filename == NULL) {
  775. fprintf(stderr, "WARNING: failed to find %s\n", default_dsdt);
  776. return;
  777. }
  778. arg = g_strdup_printf("file=%s", filename);
  779. if (acpi_table_add(arg) != 0) {
  780. fprintf(stderr, "WARNING: failed to load %s\n", filename);
  781. }
  782. g_free(arg);
  783. g_free(filename);
  784. }
  785. void *pc_memory_init(MemoryRegion *system_memory,
  786. const char *kernel_filename,
  787. const char *kernel_cmdline,
  788. const char *initrd_filename,
  789. ram_addr_t below_4g_mem_size,
  790. ram_addr_t above_4g_mem_size,
  791. MemoryRegion *rom_memory,
  792. MemoryRegion **ram_memory)
  793. {
  794. int linux_boot, i;
  795. MemoryRegion *ram, *option_rom_mr;
  796. MemoryRegion *ram_below_4g, *ram_above_4g;
  797. void *fw_cfg;
  798. linux_boot = (kernel_filename != NULL);
  799. /* Allocate RAM. We allocate it as a single memory region and use
  800. * aliases to address portions of it, mostly for backwards compatibility
  801. * with older qemus that used qemu_ram_alloc().
  802. */
  803. ram = g_malloc(sizeof(*ram));
  804. memory_region_init_ram(ram, "pc.ram",
  805. below_4g_mem_size + above_4g_mem_size);
  806. vmstate_register_ram_global(ram);
  807. *ram_memory = ram;
  808. ram_below_4g = g_malloc(sizeof(*ram_below_4g));
  809. memory_region_init_alias(ram_below_4g, "ram-below-4g", ram,
  810. 0, below_4g_mem_size);
  811. memory_region_add_subregion(system_memory, 0, ram_below_4g);
  812. if (above_4g_mem_size > 0) {
  813. ram_above_4g = g_malloc(sizeof(*ram_above_4g));
  814. memory_region_init_alias(ram_above_4g, "ram-above-4g", ram,
  815. below_4g_mem_size, above_4g_mem_size);
  816. memory_region_add_subregion(system_memory, 0x100000000ULL,
  817. ram_above_4g);
  818. }
  819. /* Initialize PC system firmware */
  820. pc_system_firmware_init(rom_memory);
  821. option_rom_mr = g_malloc(sizeof(*option_rom_mr));
  822. memory_region_init_ram(option_rom_mr, "pc.rom", PC_ROM_SIZE);
  823. vmstate_register_ram_global(option_rom_mr);
  824. memory_region_add_subregion_overlap(rom_memory,
  825. PC_ROM_MIN_VGA,
  826. option_rom_mr,
  827. 1);
  828. fw_cfg = bochs_bios_init();
  829. rom_set_fw(fw_cfg);
  830. if (linux_boot) {
  831. load_linux(fw_cfg, kernel_filename, initrd_filename, kernel_cmdline, below_4g_mem_size);
  832. }
  833. for (i = 0; i < nb_option_roms; i++) {
  834. rom_add_option(option_rom[i].name, option_rom[i].bootindex);
  835. }
  836. return fw_cfg;
  837. }
  838. qemu_irq *pc_allocate_cpu_irq(void)
  839. {
  840. return qemu_allocate_irqs(pic_irq_request, NULL, 1);
  841. }
  842. DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
  843. {
  844. DeviceState *dev = NULL;
  845. if (pci_bus) {
  846. PCIDevice *pcidev = pci_vga_init(pci_bus);
  847. dev = pcidev ? &pcidev->qdev : NULL;
  848. } else if (isa_bus) {
  849. ISADevice *isadev = isa_vga_init(isa_bus);
  850. dev = isadev ? &isadev->qdev : NULL;
  851. }
  852. return dev;
  853. }
  854. static void cpu_request_exit(void *opaque, int irq, int level)
  855. {
  856. CPUX86State *env = cpu_single_env;
  857. if (env && level) {
  858. cpu_exit(env);
  859. }
  860. }
  861. static const MemoryRegionOps ioport80_io_ops = {
  862. .write = ioport80_write,
  863. .read = ioport80_read,
  864. .endianness = DEVICE_NATIVE_ENDIAN,
  865. .impl = {
  866. .min_access_size = 1,
  867. .max_access_size = 1,
  868. },
  869. };
  870. static const MemoryRegionOps ioportF0_io_ops = {
  871. .write = ioportF0_write,
  872. .read = ioportF0_read,
  873. .endianness = DEVICE_NATIVE_ENDIAN,
  874. .impl = {
  875. .min_access_size = 1,
  876. .max_access_size = 1,
  877. },
  878. };
  879. void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
  880. ISADevice **rtc_state,
  881. ISADevice **floppy,
  882. bool no_vmport)
  883. {
  884. int i;
  885. DriveInfo *fd[MAX_FD];
  886. DeviceState *hpet = NULL;
  887. int pit_isa_irq = 0;
  888. qemu_irq pit_alt_irq = NULL;
  889. qemu_irq rtc_irq = NULL;
  890. qemu_irq *a20_line;
  891. ISADevice *i8042, *port92, *vmmouse, *pit = NULL;
  892. qemu_irq *cpu_exit_irq;
  893. MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
  894. MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
  895. memory_region_init_io(ioport80_io, &ioport80_io_ops, NULL, "ioport80", 1);
  896. memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
  897. memory_region_init_io(ioportF0_io, &ioportF0_io_ops, NULL, "ioportF0", 1);
  898. memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
  899. /*
  900. * Check if an HPET shall be created.
  901. *
  902. * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
  903. * when the HPET wants to take over. Thus we have to disable the latter.
  904. */
  905. if (!no_hpet && (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) {
  906. hpet = sysbus_try_create_simple("hpet", HPET_BASE, NULL);
  907. if (hpet) {
  908. for (i = 0; i < GSI_NUM_PINS; i++) {
  909. sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
  910. }
  911. pit_isa_irq = -1;
  912. pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
  913. rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
  914. }
  915. }
  916. *rtc_state = rtc_init(isa_bus, 2000, rtc_irq);
  917. qemu_register_boot_set(pc_boot_set, *rtc_state);
  918. if (!xen_enabled()) {
  919. if (kvm_irqchip_in_kernel()) {
  920. pit = kvm_pit_init(isa_bus, 0x40);
  921. } else {
  922. pit = pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
  923. }
  924. if (hpet) {
  925. /* connect PIT to output control line of the HPET */
  926. qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(&pit->qdev, 0));
  927. }
  928. pcspk_init(isa_bus, pit);
  929. }
  930. for(i = 0; i < MAX_SERIAL_PORTS; i++) {
  931. if (serial_hds[i]) {
  932. serial_isa_init(isa_bus, i, serial_hds[i]);
  933. }
  934. }
  935. for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
  936. if (parallel_hds[i]) {
  937. parallel_init(isa_bus, i, parallel_hds[i]);
  938. }
  939. }
  940. a20_line = qemu_allocate_irqs(handle_a20_line_change,
  941. x86_env_get_cpu(first_cpu), 2);
  942. i8042 = isa_create_simple(isa_bus, "i8042");
  943. i8042_setup_a20_line(i8042, &a20_line[0]);
  944. if (!no_vmport) {
  945. vmport_init(isa_bus);
  946. vmmouse = isa_try_create(isa_bus, "vmmouse");
  947. } else {
  948. vmmouse = NULL;
  949. }
  950. if (vmmouse) {
  951. qdev_prop_set_ptr(&vmmouse->qdev, "ps2_mouse", i8042);
  952. qdev_init_nofail(&vmmouse->qdev);
  953. }
  954. port92 = isa_create_simple(isa_bus, "port92");
  955. port92_init(port92, &a20_line[1]);
  956. cpu_exit_irq = qemu_allocate_irqs(cpu_request_exit, NULL, 1);
  957. DMA_init(0, cpu_exit_irq);
  958. for(i = 0; i < MAX_FD; i++) {
  959. fd[i] = drive_get(IF_FLOPPY, 0, i);
  960. }
  961. *floppy = fdctrl_init_isa(isa_bus, fd);
  962. }
  963. void pc_nic_init(ISABus *isa_bus, PCIBus *pci_bus)
  964. {
  965. int i;
  966. for (i = 0; i < nb_nics; i++) {
  967. NICInfo *nd = &nd_table[i];
  968. if (!pci_bus || (nd->model && strcmp(nd->model, "ne2k_isa") == 0)) {
  969. pc_init_ne2k_isa(isa_bus, nd);
  970. } else {
  971. pci_nic_init_nofail(nd, "e1000", NULL);
  972. }
  973. }
  974. }
  975. void pc_pci_device_init(PCIBus *pci_bus)
  976. {
  977. int max_bus;
  978. int bus;
  979. max_bus = drive_get_max_bus(IF_SCSI);
  980. for (bus = 0; bus <= max_bus; bus++) {
  981. pci_create_simple(pci_bus, -1, "lsi53c895a");
  982. }
  983. }
  984. void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
  985. {
  986. DeviceState *dev;
  987. SysBusDevice *d;
  988. unsigned int i;
  989. if (kvm_irqchip_in_kernel()) {
  990. dev = qdev_create(NULL, "kvm-ioapic");
  991. } else {
  992. dev = qdev_create(NULL, "ioapic");
  993. }
  994. if (parent_name) {
  995. object_property_add_child(object_resolve_path(parent_name, NULL),
  996. "ioapic", OBJECT(dev), NULL);
  997. }
  998. qdev_init_nofail(dev);
  999. d = SYS_BUS_DEVICE(dev);
  1000. sysbus_mmio_map(d, 0, 0xfec00000);
  1001. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  1002. gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
  1003. }
  1004. }