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