pc.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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 "fdc.h"
  27. #include "pci.h"
  28. #include "block.h"
  29. #include "sysemu.h"
  30. #include "audio/audio.h"
  31. #include "net.h"
  32. #include "smbus.h"
  33. #include "boards.h"
  34. #include "console.h"
  35. #include "fw_cfg.h"
  36. #include "virtio-blk.h"
  37. #include "virtio-balloon.h"
  38. #include "virtio-console.h"
  39. #include "hpet_emul.h"
  40. /* output Bochs bios info messages */
  41. //#define DEBUG_BIOS
  42. #define BIOS_FILENAME "bios.bin"
  43. #define VGABIOS_FILENAME "vgabios.bin"
  44. #define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin"
  45. #define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
  46. /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */
  47. #define ACPI_DATA_SIZE 0x10000
  48. #define BIOS_CFG_IOPORT 0x510
  49. #define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
  50. #define MAX_IDE_BUS 2
  51. extern uint8_t *acpi_tables;
  52. extern size_t acpi_tables_len;
  53. static fdctrl_t *floppy_controller;
  54. static RTCState *rtc_state;
  55. static PITState *pit;
  56. static IOAPICState *ioapic;
  57. static PCIDevice *i440fx_state;
  58. typedef struct rom_reset_data {
  59. uint8_t *data;
  60. target_phys_addr_t addr;
  61. unsigned size;
  62. } RomResetData;
  63. static void option_rom_reset(void *_rrd)
  64. {
  65. RomResetData *rrd = _rrd;
  66. cpu_physical_memory_write_rom(rrd->addr, rrd->data, rrd->size);
  67. }
  68. static void option_rom_setup_reset(target_phys_addr_t addr, unsigned size)
  69. {
  70. RomResetData *rrd = qemu_malloc(sizeof *rrd);
  71. rrd->data = qemu_malloc(size);
  72. cpu_physical_memory_read(addr, rrd->data, size);
  73. rrd->addr = addr;
  74. rrd->size = size;
  75. qemu_register_reset(option_rom_reset, rrd);
  76. }
  77. static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
  78. {
  79. }
  80. /* MSDOS compatibility mode FPU exception support */
  81. static qemu_irq ferr_irq;
  82. /* XXX: add IGNNE support */
  83. void cpu_set_ferr(CPUX86State *s)
  84. {
  85. qemu_irq_raise(ferr_irq);
  86. }
  87. static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
  88. {
  89. qemu_irq_lower(ferr_irq);
  90. }
  91. /* TSC handling */
  92. uint64_t cpu_get_tsc(CPUX86State *env)
  93. {
  94. /* Note: when using kqemu, it is more logical to return the host TSC
  95. because kqemu does not trap the RDTSC instruction for
  96. performance reasons */
  97. #ifdef USE_KQEMU
  98. if (env->kqemu_enabled) {
  99. return cpu_get_real_ticks();
  100. } else
  101. #endif
  102. {
  103. return cpu_get_ticks();
  104. }
  105. }
  106. /* SMM support */
  107. void cpu_smm_update(CPUState *env)
  108. {
  109. if (i440fx_state && env == first_cpu)
  110. i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1);
  111. }
  112. /* IRQ handling */
  113. int cpu_get_pic_interrupt(CPUState *env)
  114. {
  115. int intno;
  116. intno = apic_get_interrupt(env);
  117. if (intno >= 0) {
  118. /* set irq request if a PIC irq is still pending */
  119. /* XXX: improve that */
  120. pic_update_irq(isa_pic);
  121. return intno;
  122. }
  123. /* read the irq from the PIC */
  124. if (!apic_accept_pic_intr(env))
  125. return -1;
  126. intno = pic_read_irq(isa_pic);
  127. return intno;
  128. }
  129. static void pic_irq_request(void *opaque, int irq, int level)
  130. {
  131. CPUState *env = first_cpu;
  132. if (env->apic_state) {
  133. while (env) {
  134. if (apic_accept_pic_intr(env))
  135. apic_deliver_pic_intr(env, level);
  136. env = env->next_cpu;
  137. }
  138. } else {
  139. if (level)
  140. cpu_interrupt(env, CPU_INTERRUPT_HARD);
  141. else
  142. cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
  143. }
  144. }
  145. /* PC cmos mappings */
  146. #define REG_EQUIPMENT_BYTE 0x14
  147. static int cmos_get_fd_drive_type(int fd0)
  148. {
  149. int val;
  150. switch (fd0) {
  151. case 0:
  152. /* 1.44 Mb 3"5 drive */
  153. val = 4;
  154. break;
  155. case 1:
  156. /* 2.88 Mb 3"5 drive */
  157. val = 5;
  158. break;
  159. case 2:
  160. /* 1.2 Mb 5"5 drive */
  161. val = 2;
  162. break;
  163. default:
  164. val = 0;
  165. break;
  166. }
  167. return val;
  168. }
  169. static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd)
  170. {
  171. RTCState *s = rtc_state;
  172. int cylinders, heads, sectors;
  173. bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
  174. rtc_set_memory(s, type_ofs, 47);
  175. rtc_set_memory(s, info_ofs, cylinders);
  176. rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
  177. rtc_set_memory(s, info_ofs + 2, heads);
  178. rtc_set_memory(s, info_ofs + 3, 0xff);
  179. rtc_set_memory(s, info_ofs + 4, 0xff);
  180. rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
  181. rtc_set_memory(s, info_ofs + 6, cylinders);
  182. rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
  183. rtc_set_memory(s, info_ofs + 8, sectors);
  184. }
  185. /* convert boot_device letter to something recognizable by the bios */
  186. static int boot_device2nibble(char boot_device)
  187. {
  188. switch(boot_device) {
  189. case 'a':
  190. case 'b':
  191. return 0x01; /* floppy boot */
  192. case 'c':
  193. return 0x02; /* hard drive boot */
  194. case 'd':
  195. return 0x03; /* CD-ROM boot */
  196. case 'n':
  197. return 0x04; /* Network boot */
  198. }
  199. return 0;
  200. }
  201. /* copy/pasted from cmos_init, should be made a general function
  202. and used there as well */
  203. static int pc_boot_set(void *opaque, const char *boot_device)
  204. {
  205. #define PC_MAX_BOOT_DEVICES 3
  206. RTCState *s = (RTCState *)opaque;
  207. int nbds, bds[3] = { 0, };
  208. int i;
  209. nbds = strlen(boot_device);
  210. if (nbds > PC_MAX_BOOT_DEVICES) {
  211. term_printf("Too many boot devices for PC\n");
  212. return(1);
  213. }
  214. for (i = 0; i < nbds; i++) {
  215. bds[i] = boot_device2nibble(boot_device[i]);
  216. if (bds[i] == 0) {
  217. term_printf("Invalid boot device for PC: '%c'\n",
  218. boot_device[i]);
  219. return(1);
  220. }
  221. }
  222. rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
  223. rtc_set_memory(s, 0x38, (bds[2] << 4));
  224. return(0);
  225. }
  226. /* hd_table must contain 4 block drivers */
  227. static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
  228. const char *boot_device, BlockDriverState **hd_table)
  229. {
  230. RTCState *s = rtc_state;
  231. int nbds, bds[3] = { 0, };
  232. int val;
  233. int fd0, fd1, nb;
  234. int i;
  235. /* various important CMOS locations needed by PC/Bochs bios */
  236. /* memory size */
  237. val = 640; /* base memory in K */
  238. rtc_set_memory(s, 0x15, val);
  239. rtc_set_memory(s, 0x16, val >> 8);
  240. val = (ram_size / 1024) - 1024;
  241. if (val > 65535)
  242. val = 65535;
  243. rtc_set_memory(s, 0x17, val);
  244. rtc_set_memory(s, 0x18, val >> 8);
  245. rtc_set_memory(s, 0x30, val);
  246. rtc_set_memory(s, 0x31, val >> 8);
  247. if (above_4g_mem_size) {
  248. rtc_set_memory(s, 0x5b, (unsigned int)above_4g_mem_size >> 16);
  249. rtc_set_memory(s, 0x5c, (unsigned int)above_4g_mem_size >> 24);
  250. rtc_set_memory(s, 0x5d, (uint64_t)above_4g_mem_size >> 32);
  251. }
  252. if (ram_size > (16 * 1024 * 1024))
  253. val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
  254. else
  255. val = 0;
  256. if (val > 65535)
  257. val = 65535;
  258. rtc_set_memory(s, 0x34, val);
  259. rtc_set_memory(s, 0x35, val >> 8);
  260. /* set the number of CPU */
  261. rtc_set_memory(s, 0x5f, smp_cpus - 1);
  262. /* set boot devices, and disable floppy signature check if requested */
  263. #define PC_MAX_BOOT_DEVICES 3
  264. nbds = strlen(boot_device);
  265. if (nbds > PC_MAX_BOOT_DEVICES) {
  266. fprintf(stderr, "Too many boot devices for PC\n");
  267. exit(1);
  268. }
  269. for (i = 0; i < nbds; i++) {
  270. bds[i] = boot_device2nibble(boot_device[i]);
  271. if (bds[i] == 0) {
  272. fprintf(stderr, "Invalid boot device for PC: '%c'\n",
  273. boot_device[i]);
  274. exit(1);
  275. }
  276. }
  277. rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
  278. rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
  279. /* floppy type */
  280. fd0 = fdctrl_get_drive_type(floppy_controller, 0);
  281. fd1 = fdctrl_get_drive_type(floppy_controller, 1);
  282. val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
  283. rtc_set_memory(s, 0x10, val);
  284. val = 0;
  285. nb = 0;
  286. if (fd0 < 3)
  287. nb++;
  288. if (fd1 < 3)
  289. nb++;
  290. switch (nb) {
  291. case 0:
  292. break;
  293. case 1:
  294. val |= 0x01; /* 1 drive, ready for boot */
  295. break;
  296. case 2:
  297. val |= 0x41; /* 2 drives, ready for boot */
  298. break;
  299. }
  300. val |= 0x02; /* FPU is there */
  301. val |= 0x04; /* PS/2 mouse installed */
  302. rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
  303. /* hard drives */
  304. rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
  305. if (hd_table[0])
  306. cmos_init_hd(0x19, 0x1b, hd_table[0]);
  307. if (hd_table[1])
  308. cmos_init_hd(0x1a, 0x24, hd_table[1]);
  309. val = 0;
  310. for (i = 0; i < 4; i++) {
  311. if (hd_table[i]) {
  312. int cylinders, heads, sectors, translation;
  313. /* NOTE: bdrv_get_geometry_hint() returns the physical
  314. geometry. It is always such that: 1 <= sects <= 63, 1
  315. <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
  316. geometry can be different if a translation is done. */
  317. translation = bdrv_get_translation_hint(hd_table[i]);
  318. if (translation == BIOS_ATA_TRANSLATION_AUTO) {
  319. bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, &sectors);
  320. if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
  321. /* No translation. */
  322. translation = 0;
  323. } else {
  324. /* LBA translation. */
  325. translation = 1;
  326. }
  327. } else {
  328. translation--;
  329. }
  330. val |= translation << (i * 2);
  331. }
  332. }
  333. rtc_set_memory(s, 0x39, val);
  334. }
  335. void ioport_set_a20(int enable)
  336. {
  337. /* XXX: send to all CPUs ? */
  338. cpu_x86_set_a20(first_cpu, enable);
  339. }
  340. int ioport_get_a20(void)
  341. {
  342. return ((first_cpu->a20_mask >> 20) & 1);
  343. }
  344. static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
  345. {
  346. ioport_set_a20((val >> 1) & 1);
  347. /* XXX: bit 0 is fast reset */
  348. }
  349. static uint32_t ioport92_read(void *opaque, uint32_t addr)
  350. {
  351. return ioport_get_a20() << 1;
  352. }
  353. /***********************************************************/
  354. /* Bochs BIOS debug ports */
  355. static void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
  356. {
  357. static const char shutdown_str[8] = "Shutdown";
  358. static int shutdown_index = 0;
  359. switch(addr) {
  360. /* Bochs BIOS messages */
  361. case 0x400:
  362. case 0x401:
  363. fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
  364. exit(1);
  365. case 0x402:
  366. case 0x403:
  367. #ifdef DEBUG_BIOS
  368. fprintf(stderr, "%c", val);
  369. #endif
  370. break;
  371. case 0x8900:
  372. /* same as Bochs power off */
  373. if (val == shutdown_str[shutdown_index]) {
  374. shutdown_index++;
  375. if (shutdown_index == 8) {
  376. shutdown_index = 0;
  377. qemu_system_shutdown_request();
  378. }
  379. } else {
  380. shutdown_index = 0;
  381. }
  382. break;
  383. /* LGPL'ed VGA BIOS messages */
  384. case 0x501:
  385. case 0x502:
  386. fprintf(stderr, "VGA BIOS panic, line %d\n", val);
  387. exit(1);
  388. case 0x500:
  389. case 0x503:
  390. #ifdef DEBUG_BIOS
  391. fprintf(stderr, "%c", val);
  392. #endif
  393. break;
  394. }
  395. }
  396. static void bochs_bios_init(void)
  397. {
  398. void *fw_cfg;
  399. register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
  400. register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
  401. register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
  402. register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
  403. register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
  404. register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
  405. register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
  406. register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
  407. register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
  408. fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
  409. fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
  410. fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
  411. fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES, acpi_tables, acpi_tables_len);
  412. }
  413. /* Generate an initial boot sector which sets state and jump to
  414. a specified vector */
  415. static void generate_bootsect(target_phys_addr_t option_rom,
  416. uint32_t gpr[8], uint16_t segs[6], uint16_t ip)
  417. {
  418. uint8_t rom[512], *p, *reloc;
  419. uint8_t sum;
  420. int i;
  421. memset(rom, 0, sizeof(rom));
  422. p = rom;
  423. /* Make sure we have an option rom signature */
  424. *p++ = 0x55;
  425. *p++ = 0xaa;
  426. /* ROM size in sectors*/
  427. *p++ = 1;
  428. /* Hook int19 */
  429. *p++ = 0x50; /* push ax */
  430. *p++ = 0x1e; /* push ds */
  431. *p++ = 0x31; *p++ = 0xc0; /* xor ax, ax */
  432. *p++ = 0x8e; *p++ = 0xd8; /* mov ax, ds */
  433. *p++ = 0xc7; *p++ = 0x06; /* movvw _start,0x64 */
  434. *p++ = 0x64; *p++ = 0x00;
  435. reloc = p;
  436. *p++ = 0x00; *p++ = 0x00;
  437. *p++ = 0x8c; *p++ = 0x0e; /* mov cs,0x66 */
  438. *p++ = 0x66; *p++ = 0x00;
  439. *p++ = 0x1f; /* pop ds */
  440. *p++ = 0x58; /* pop ax */
  441. *p++ = 0xcb; /* lret */
  442. /* Actual code */
  443. *reloc = (p - rom);
  444. *p++ = 0xfa; /* CLI */
  445. *p++ = 0xfc; /* CLD */
  446. for (i = 0; i < 6; i++) {
  447. if (i == 1) /* Skip CS */
  448. continue;
  449. *p++ = 0xb8; /* MOV AX,imm16 */
  450. *p++ = segs[i];
  451. *p++ = segs[i] >> 8;
  452. *p++ = 0x8e; /* MOV <seg>,AX */
  453. *p++ = 0xc0 + (i << 3);
  454. }
  455. for (i = 0; i < 8; i++) {
  456. *p++ = 0x66; /* 32-bit operand size */
  457. *p++ = 0xb8 + i; /* MOV <reg>,imm32 */
  458. *p++ = gpr[i];
  459. *p++ = gpr[i] >> 8;
  460. *p++ = gpr[i] >> 16;
  461. *p++ = gpr[i] >> 24;
  462. }
  463. *p++ = 0xea; /* JMP FAR */
  464. *p++ = ip; /* IP */
  465. *p++ = ip >> 8;
  466. *p++ = segs[1]; /* CS */
  467. *p++ = segs[1] >> 8;
  468. /* sign rom */
  469. sum = 0;
  470. for (i = 0; i < (sizeof(rom) - 1); i++)
  471. sum += rom[i];
  472. rom[sizeof(rom) - 1] = -sum;
  473. cpu_physical_memory_write_rom(option_rom, rom, sizeof(rom));
  474. option_rom_setup_reset(option_rom, sizeof (rom));
  475. }
  476. static long get_file_size(FILE *f)
  477. {
  478. long where, size;
  479. /* XXX: on Unix systems, using fstat() probably makes more sense */
  480. where = ftell(f);
  481. fseek(f, 0, SEEK_END);
  482. size = ftell(f);
  483. fseek(f, where, SEEK_SET);
  484. return size;
  485. }
  486. static void load_linux(target_phys_addr_t option_rom,
  487. const char *kernel_filename,
  488. const char *initrd_filename,
  489. const char *kernel_cmdline,
  490. target_phys_addr_t max_ram_size)
  491. {
  492. uint16_t protocol;
  493. uint32_t gpr[8];
  494. uint16_t seg[6];
  495. uint16_t real_seg;
  496. int setup_size, kernel_size, initrd_size, cmdline_size;
  497. uint32_t initrd_max;
  498. uint8_t header[1024];
  499. target_phys_addr_t real_addr, prot_addr, cmdline_addr, initrd_addr;
  500. FILE *f, *fi;
  501. /* Align to 16 bytes as a paranoia measure */
  502. cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
  503. /* load the kernel header */
  504. f = fopen(kernel_filename, "rb");
  505. if (!f || !(kernel_size = get_file_size(f)) ||
  506. fread(header, 1, 1024, f) != 1024) {
  507. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  508. kernel_filename);
  509. exit(1);
  510. }
  511. /* kernel protocol version */
  512. #if 0
  513. fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
  514. #endif
  515. if (ldl_p(header+0x202) == 0x53726448)
  516. protocol = lduw_p(header+0x206);
  517. else
  518. protocol = 0;
  519. if (protocol < 0x200 || !(header[0x211] & 0x01)) {
  520. /* Low kernel */
  521. real_addr = 0x90000;
  522. cmdline_addr = 0x9a000 - cmdline_size;
  523. prot_addr = 0x10000;
  524. } else if (protocol < 0x202) {
  525. /* High but ancient kernel */
  526. real_addr = 0x90000;
  527. cmdline_addr = 0x9a000 - cmdline_size;
  528. prot_addr = 0x100000;
  529. } else {
  530. /* High and recent kernel */
  531. real_addr = 0x10000;
  532. cmdline_addr = 0x20000;
  533. prot_addr = 0x100000;
  534. }
  535. #if 0
  536. fprintf(stderr,
  537. "qemu: real_addr = 0x" TARGET_FMT_plx "\n"
  538. "qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n"
  539. "qemu: prot_addr = 0x" TARGET_FMT_plx "\n",
  540. real_addr,
  541. cmdline_addr,
  542. prot_addr);
  543. #endif
  544. /* highest address for loading the initrd */
  545. if (protocol >= 0x203)
  546. initrd_max = ldl_p(header+0x22c);
  547. else
  548. initrd_max = 0x37ffffff;
  549. if (initrd_max >= max_ram_size-ACPI_DATA_SIZE)
  550. initrd_max = max_ram_size-ACPI_DATA_SIZE-1;
  551. /* kernel command line */
  552. pstrcpy_targphys(cmdline_addr, 4096, kernel_cmdline);
  553. if (protocol >= 0x202) {
  554. stl_p(header+0x228, cmdline_addr);
  555. } else {
  556. stw_p(header+0x20, 0xA33F);
  557. stw_p(header+0x22, cmdline_addr-real_addr);
  558. }
  559. /* loader type */
  560. /* High nybble = B reserved for Qemu; low nybble is revision number.
  561. If this code is substantially changed, you may want to consider
  562. incrementing the revision. */
  563. if (protocol >= 0x200)
  564. header[0x210] = 0xB0;
  565. /* heap */
  566. if (protocol >= 0x201) {
  567. header[0x211] |= 0x80; /* CAN_USE_HEAP */
  568. stw_p(header+0x224, cmdline_addr-real_addr-0x200);
  569. }
  570. /* load initrd */
  571. if (initrd_filename) {
  572. if (protocol < 0x200) {
  573. fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
  574. exit(1);
  575. }
  576. fi = fopen(initrd_filename, "rb");
  577. if (!fi) {
  578. fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
  579. initrd_filename);
  580. exit(1);
  581. }
  582. initrd_size = get_file_size(fi);
  583. initrd_addr = (initrd_max-initrd_size) & ~4095;
  584. if (!fread_targphys_ok(initrd_addr, initrd_size, fi)) {
  585. fprintf(stderr, "qemu: read error on initial ram disk '%s'\n",
  586. initrd_filename);
  587. exit(1);
  588. }
  589. fclose(fi);
  590. stl_p(header+0x218, initrd_addr);
  591. stl_p(header+0x21c, initrd_size);
  592. }
  593. /* store the finalized header and load the rest of the kernel */
  594. cpu_physical_memory_write(real_addr, header, 1024);
  595. setup_size = header[0x1f1];
  596. if (setup_size == 0)
  597. setup_size = 4;
  598. setup_size = (setup_size+1)*512;
  599. kernel_size -= setup_size; /* Size of protected-mode code */
  600. if (!fread_targphys_ok(real_addr+1024, setup_size-1024, f) ||
  601. !fread_targphys_ok(prot_addr, kernel_size, f)) {
  602. fprintf(stderr, "qemu: read error on kernel '%s'\n",
  603. kernel_filename);
  604. exit(1);
  605. }
  606. fclose(f);
  607. /* generate bootsector to set up the initial register state */
  608. real_seg = real_addr >> 4;
  609. seg[0] = seg[2] = seg[3] = seg[4] = seg[4] = real_seg;
  610. seg[1] = real_seg+0x20; /* CS */
  611. memset(gpr, 0, sizeof gpr);
  612. gpr[4] = cmdline_addr-real_addr-16; /* SP (-16 is paranoia) */
  613. option_rom_setup_reset(real_addr, setup_size);
  614. option_rom_setup_reset(prot_addr, kernel_size);
  615. option_rom_setup_reset(cmdline_addr, cmdline_size);
  616. if (initrd_filename)
  617. option_rom_setup_reset(initrd_addr, initrd_size);
  618. generate_bootsect(option_rom, gpr, seg, 0);
  619. }
  620. static void main_cpu_reset(void *opaque)
  621. {
  622. CPUState *env = opaque;
  623. cpu_reset(env);
  624. }
  625. static const int ide_iobase[2] = { 0x1f0, 0x170 };
  626. static const int ide_iobase2[2] = { 0x3f6, 0x376 };
  627. static const int ide_irq[2] = { 14, 15 };
  628. #define NE2000_NB_MAX 6
  629. static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
  630. static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
  631. static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
  632. static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
  633. static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
  634. static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
  635. #ifdef HAS_AUDIO
  636. static void audio_init (PCIBus *pci_bus, qemu_irq *pic)
  637. {
  638. struct soundhw *c;
  639. int audio_enabled = 0;
  640. for (c = soundhw; !audio_enabled && c->name; ++c) {
  641. audio_enabled = c->enabled;
  642. }
  643. if (audio_enabled) {
  644. AudioState *s;
  645. s = AUD_init ();
  646. if (s) {
  647. for (c = soundhw; c->name; ++c) {
  648. if (c->enabled) {
  649. if (c->isa) {
  650. c->init.init_isa (s, pic);
  651. }
  652. else {
  653. if (pci_bus) {
  654. c->init.init_pci (pci_bus, s);
  655. }
  656. }
  657. }
  658. }
  659. }
  660. }
  661. }
  662. #endif
  663. static void pc_init_ne2k_isa(NICInfo *nd, qemu_irq *pic)
  664. {
  665. static int nb_ne2k = 0;
  666. if (nb_ne2k == NE2000_NB_MAX)
  667. return;
  668. isa_ne2000_init(ne2000_io[nb_ne2k], pic[ne2000_irq[nb_ne2k]], nd);
  669. nb_ne2k++;
  670. }
  671. /* PC hardware initialisation */
  672. static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
  673. const char *boot_device,
  674. const char *kernel_filename, const char *kernel_cmdline,
  675. const char *initrd_filename,
  676. int pci_enabled, const char *cpu_model)
  677. {
  678. char buf[1024];
  679. int ret, linux_boot, i;
  680. ram_addr_t ram_addr, vga_ram_addr, bios_offset, vga_bios_offset;
  681. ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
  682. int bios_size, isa_bios_size, vga_bios_size;
  683. PCIBus *pci_bus;
  684. int piix3_devfn = -1;
  685. CPUState *env;
  686. qemu_irq *cpu_irq;
  687. qemu_irq *i8259;
  688. int index;
  689. BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
  690. BlockDriverState *fd[MAX_FD];
  691. if (ram_size >= 0xe0000000 ) {
  692. above_4g_mem_size = ram_size - 0xe0000000;
  693. below_4g_mem_size = 0xe0000000;
  694. } else {
  695. below_4g_mem_size = ram_size;
  696. }
  697. linux_boot = (kernel_filename != NULL);
  698. /* init CPUs */
  699. if (cpu_model == NULL) {
  700. #ifdef TARGET_X86_64
  701. cpu_model = "qemu64";
  702. #else
  703. cpu_model = "qemu32";
  704. #endif
  705. }
  706. for(i = 0; i < smp_cpus; i++) {
  707. env = cpu_init(cpu_model);
  708. if (!env) {
  709. fprintf(stderr, "Unable to find x86 CPU definition\n");
  710. exit(1);
  711. }
  712. if (i != 0)
  713. env->halted = 1;
  714. if (smp_cpus > 1) {
  715. /* XXX: enable it in all cases */
  716. env->cpuid_features |= CPUID_APIC;
  717. }
  718. qemu_register_reset(main_cpu_reset, env);
  719. if (pci_enabled) {
  720. apic_init(env);
  721. }
  722. }
  723. vmport_init();
  724. /* allocate RAM */
  725. ram_addr = qemu_ram_alloc(0xa0000);
  726. cpu_register_physical_memory(0, 0xa0000, ram_addr);
  727. /* Allocate, even though we won't register, so we don't break the
  728. * phys_ram_base + PA assumption. This range includes vga (0xa0000 - 0xc0000),
  729. * and some bios areas, which will be registered later
  730. */
  731. ram_addr = qemu_ram_alloc(0x100000 - 0xa0000);
  732. ram_addr = qemu_ram_alloc(below_4g_mem_size - 0x100000);
  733. cpu_register_physical_memory(0x100000,
  734. below_4g_mem_size - 0x100000,
  735. ram_addr);
  736. /* above 4giga memory allocation */
  737. if (above_4g_mem_size > 0) {
  738. ram_addr = qemu_ram_alloc(above_4g_mem_size);
  739. cpu_register_physical_memory(0x100000000ULL,
  740. above_4g_mem_size,
  741. ram_addr);
  742. }
  743. /* allocate VGA RAM */
  744. vga_ram_addr = qemu_ram_alloc(vga_ram_size);
  745. /* BIOS load */
  746. if (bios_name == NULL)
  747. bios_name = BIOS_FILENAME;
  748. snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
  749. bios_size = get_image_size(buf);
  750. if (bios_size <= 0 ||
  751. (bios_size % 65536) != 0) {
  752. goto bios_error;
  753. }
  754. bios_offset = qemu_ram_alloc(bios_size);
  755. ret = load_image(buf, phys_ram_base + bios_offset);
  756. if (ret != bios_size) {
  757. bios_error:
  758. fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", buf);
  759. exit(1);
  760. }
  761. if (cirrus_vga_enabled || std_vga_enabled || vmsvga_enabled) {
  762. /* VGA BIOS load */
  763. if (cirrus_vga_enabled) {
  764. snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_CIRRUS_FILENAME);
  765. } else {
  766. snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
  767. }
  768. vga_bios_size = get_image_size(buf);
  769. if (vga_bios_size <= 0 || vga_bios_size > 65536)
  770. goto vga_bios_error;
  771. vga_bios_offset = qemu_ram_alloc(65536);
  772. ret = load_image(buf, phys_ram_base + vga_bios_offset);
  773. if (ret != vga_bios_size) {
  774. vga_bios_error:
  775. fprintf(stderr, "qemu: could not load VGA BIOS '%s'\n", buf);
  776. exit(1);
  777. }
  778. /* setup basic memory access */
  779. cpu_register_physical_memory(0xc0000, 0x10000,
  780. vga_bios_offset | IO_MEM_ROM);
  781. }
  782. /* map the last 128KB of the BIOS in ISA space */
  783. isa_bios_size = bios_size;
  784. if (isa_bios_size > (128 * 1024))
  785. isa_bios_size = 128 * 1024;
  786. cpu_register_physical_memory(0x100000 - isa_bios_size,
  787. isa_bios_size,
  788. (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
  789. {
  790. ram_addr_t option_rom_offset;
  791. int size, offset;
  792. offset = 0;
  793. if (linux_boot) {
  794. option_rom_offset = qemu_ram_alloc(TARGET_PAGE_SIZE);
  795. cpu_register_physical_memory(0xd0000, TARGET_PAGE_SIZE,
  796. option_rom_offset);
  797. load_linux(0xd0000,
  798. kernel_filename, initrd_filename, kernel_cmdline, below_4g_mem_size);
  799. offset = TARGET_PAGE_SIZE;
  800. }
  801. for (i = 0; i < nb_option_roms; i++) {
  802. size = get_image_size(option_rom[i]);
  803. if (size < 0) {
  804. fprintf(stderr, "Could not load option rom '%s'\n",
  805. option_rom[i]);
  806. exit(1);
  807. }
  808. if (size > (0x10000 - offset))
  809. goto option_rom_error;
  810. option_rom_offset = qemu_ram_alloc(size);
  811. ret = load_image(option_rom[i], phys_ram_base + option_rom_offset);
  812. if (ret != size) {
  813. option_rom_error:
  814. fprintf(stderr, "Too many option ROMS\n");
  815. exit(1);
  816. }
  817. size = (size + 4095) & ~4095;
  818. cpu_register_physical_memory(0xd0000 + offset,
  819. size, option_rom_offset | IO_MEM_ROM);
  820. offset += size;
  821. }
  822. }
  823. /* map all the bios at the top of memory */
  824. cpu_register_physical_memory((uint32_t)(-bios_size),
  825. bios_size, bios_offset | IO_MEM_ROM);
  826. bochs_bios_init();
  827. cpu_irq = qemu_allocate_irqs(pic_irq_request, NULL, 1);
  828. i8259 = i8259_init(cpu_irq[0]);
  829. ferr_irq = i8259[13];
  830. if (pci_enabled) {
  831. pci_bus = i440fx_init(&i440fx_state, i8259);
  832. piix3_devfn = piix3_init(pci_bus, -1);
  833. } else {
  834. pci_bus = NULL;
  835. }
  836. /* init basic PC hardware */
  837. register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
  838. register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
  839. if (cirrus_vga_enabled) {
  840. if (pci_enabled) {
  841. pci_cirrus_vga_init(pci_bus,
  842. phys_ram_base + vga_ram_addr,
  843. vga_ram_addr, vga_ram_size);
  844. } else {
  845. isa_cirrus_vga_init(phys_ram_base + vga_ram_addr,
  846. vga_ram_addr, vga_ram_size);
  847. }
  848. } else if (vmsvga_enabled) {
  849. if (pci_enabled)
  850. pci_vmsvga_init(pci_bus, phys_ram_base + vga_ram_addr,
  851. vga_ram_addr, vga_ram_size);
  852. else
  853. fprintf(stderr, "%s: vmware_vga: no PCI bus\n", __FUNCTION__);
  854. } else if (std_vga_enabled) {
  855. if (pci_enabled) {
  856. pci_vga_init(pci_bus, phys_ram_base + vga_ram_addr,
  857. vga_ram_addr, vga_ram_size, 0, 0);
  858. } else {
  859. isa_vga_init(phys_ram_base + vga_ram_addr,
  860. vga_ram_addr, vga_ram_size);
  861. }
  862. }
  863. rtc_state = rtc_init(0x70, i8259[8], 2000);
  864. qemu_register_boot_set(pc_boot_set, rtc_state);
  865. register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
  866. register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
  867. if (pci_enabled) {
  868. ioapic = ioapic_init();
  869. }
  870. pit = pit_init(0x40, i8259[0]);
  871. pcspk_init(pit);
  872. if (!no_hpet) {
  873. hpet_init(i8259);
  874. }
  875. if (pci_enabled) {
  876. pic_set_alt_irq_func(isa_pic, ioapic_set_irq, ioapic);
  877. }
  878. for(i = 0; i < MAX_SERIAL_PORTS; i++) {
  879. if (serial_hds[i]) {
  880. serial_init(serial_io[i], i8259[serial_irq[i]], 115200,
  881. serial_hds[i]);
  882. }
  883. }
  884. for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
  885. if (parallel_hds[i]) {
  886. parallel_init(parallel_io[i], i8259[parallel_irq[i]],
  887. parallel_hds[i]);
  888. }
  889. }
  890. for(i = 0; i < nb_nics; i++) {
  891. NICInfo *nd = &nd_table[i];
  892. if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
  893. pc_init_ne2k_isa(nd, i8259);
  894. else
  895. pci_nic_init(pci_bus, nd, -1, "ne2k_pci");
  896. }
  897. qemu_system_hot_add_init();
  898. if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
  899. fprintf(stderr, "qemu: too many IDE bus\n");
  900. exit(1);
  901. }
  902. for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
  903. index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
  904. if (index != -1)
  905. hd[i] = drives_table[index].bdrv;
  906. else
  907. hd[i] = NULL;
  908. }
  909. if (pci_enabled) {
  910. pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1, i8259);
  911. } else {
  912. for(i = 0; i < MAX_IDE_BUS; i++) {
  913. isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
  914. hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
  915. }
  916. }
  917. i8042_init(i8259[1], i8259[12], 0x60);
  918. DMA_init(0);
  919. #ifdef HAS_AUDIO
  920. audio_init(pci_enabled ? pci_bus : NULL, i8259);
  921. #endif
  922. for(i = 0; i < MAX_FD; i++) {
  923. index = drive_get_index(IF_FLOPPY, 0, i);
  924. if (index != -1)
  925. fd[i] = drives_table[index].bdrv;
  926. else
  927. fd[i] = NULL;
  928. }
  929. floppy_controller = fdctrl_init(i8259[6], 2, 0, 0x3f0, fd);
  930. cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device, hd);
  931. if (pci_enabled && usb_enabled) {
  932. usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
  933. }
  934. if (pci_enabled && acpi_enabled) {
  935. uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
  936. i2c_bus *smbus;
  937. /* TODO: Populate SPD eeprom data. */
  938. smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100, i8259[9]);
  939. for (i = 0; i < 8; i++) {
  940. smbus_eeprom_device_init(smbus, 0x50 + i, eeprom_buf + (i * 256));
  941. }
  942. }
  943. if (i440fx_state) {
  944. i440fx_init_memory_mappings(i440fx_state);
  945. }
  946. if (pci_enabled) {
  947. int max_bus;
  948. int bus, unit;
  949. void *scsi;
  950. max_bus = drive_get_max_bus(IF_SCSI);
  951. for (bus = 0; bus <= max_bus; bus++) {
  952. scsi = lsi_scsi_init(pci_bus, -1);
  953. for (unit = 0; unit < LSI_MAX_DEVS; unit++) {
  954. index = drive_get_index(IF_SCSI, bus, unit);
  955. if (index == -1)
  956. continue;
  957. lsi_scsi_attach(scsi, drives_table[index].bdrv, unit);
  958. }
  959. }
  960. }
  961. /* Add virtio block devices */
  962. if (pci_enabled) {
  963. int index;
  964. int unit_id = 0;
  965. while ((index = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
  966. virtio_blk_init(pci_bus, drives_table[index].bdrv);
  967. unit_id++;
  968. }
  969. }
  970. /* Add virtio balloon device */
  971. if (pci_enabled)
  972. virtio_balloon_init(pci_bus);
  973. /* Add virtio console devices */
  974. if (pci_enabled) {
  975. for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
  976. if (virtcon_hds[i])
  977. virtio_console_init(pci_bus, virtcon_hds[i]);
  978. }
  979. }
  980. }
  981. static void pc_init_pci(ram_addr_t ram_size, int vga_ram_size,
  982. const char *boot_device,
  983. const char *kernel_filename,
  984. const char *kernel_cmdline,
  985. const char *initrd_filename,
  986. const char *cpu_model)
  987. {
  988. pc_init1(ram_size, vga_ram_size, boot_device,
  989. kernel_filename, kernel_cmdline,
  990. initrd_filename, 1, cpu_model);
  991. }
  992. static void pc_init_isa(ram_addr_t ram_size, int vga_ram_size,
  993. const char *boot_device,
  994. const char *kernel_filename,
  995. const char *kernel_cmdline,
  996. const char *initrd_filename,
  997. const char *cpu_model)
  998. {
  999. pc_init1(ram_size, vga_ram_size, boot_device,
  1000. kernel_filename, kernel_cmdline,
  1001. initrd_filename, 0, cpu_model);
  1002. }
  1003. /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
  1004. BIOS will read it and start S3 resume at POST Entry */
  1005. void cmos_set_s3_resume(void)
  1006. {
  1007. if (rtc_state)
  1008. rtc_set_memory(rtc_state, 0xF, 0xFE);
  1009. }
  1010. QEMUMachine pc_machine = {
  1011. .name = "pc",
  1012. .desc = "Standard PC",
  1013. .init = pc_init_pci,
  1014. .ram_require = VGA_RAM_SIZE + PC_MAX_BIOS_SIZE,
  1015. .max_cpus = 255,
  1016. };
  1017. QEMUMachine isapc_machine = {
  1018. .name = "isapc",
  1019. .desc = "ISA-only PC",
  1020. .init = pc_init_isa,
  1021. .ram_require = VGA_RAM_SIZE + PC_MAX_BIOS_SIZE,
  1022. .max_cpus = 1,
  1023. };