pc.c 32 KB

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