pc.c 31 KB

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