pc.c 33 KB

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