2 * Copyright (c) 2009 Alex Keda <admin@lissyara.su>
3 * Copyright (c) 2009-2010 Jung-uk Kim <jkim@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_x86bios.h"
33 #include <sys/param.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
43 #include <contrib/x86emu/x86emu.h>
44 #include <contrib/x86emu/x86emu_regs.h>
45 #include <compat/x86bios/x86bios.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
50 #include <machine/cpufunc.h>
55 #define X86BIOS_PAGE_SIZE 0x00001000 /* 4K */
57 #define X86BIOS_IVT_SIZE 0x00000500 /* 1K + 256 (BDA) */
58 #define X86BIOS_MEM_SIZE 0x00100000 /* 1M */
60 #define X86BIOS_IVT_BASE 0x00000000
61 #define X86BIOS_RAM_BASE 0x00001000
62 #define X86BIOS_ROM_BASE 0x000a0000
64 #define X86BIOS_ROM_SIZE (X86BIOS_MEM_SIZE - (uint32_t)x86bios_rom_phys)
65 #define X86BIOS_SEG_SIZE X86BIOS_PAGE_SIZE
67 #define X86BIOS_PAGES (X86BIOS_MEM_SIZE / X86BIOS_PAGE_SIZE)
69 #define X86BIOS_R_SS _pad2
70 #define X86BIOS_R_SP _pad3.I16_reg.x_reg
72 static struct x86emu x86bios_emu;
74 static struct mtx x86bios_lock;
76 static void *x86bios_ivt;
77 static void *x86bios_rom;
78 static void *x86bios_seg;
80 static vm_offset_t *x86bios_map;
82 static vm_paddr_t x86bios_rom_phys;
83 static vm_paddr_t x86bios_seg_phys;
85 static int x86bios_fault;
86 static uint32_t x86bios_fault_addr;
87 static uint16_t x86bios_fault_cs;
88 static uint16_t x86bios_fault_ip;
90 SYSCTL_NODE(_debug, OID_AUTO, x86bios, CTLFLAG_RD, NULL, "x86bios debugging");
91 static int x86bios_trace_call;
92 TUNABLE_INT("debug.x86bios.call", &x86bios_trace_call);
93 SYSCTL_INT(_debug_x86bios, OID_AUTO, call, CTLFLAG_RW, &x86bios_trace_call, 0,
94 "Trace far function calls");
95 static int x86bios_trace_int;
96 TUNABLE_INT("debug.x86bios.int", &x86bios_trace_int);
97 SYSCTL_INT(_debug_x86bios, OID_AUTO, int, CTLFLAG_RW, &x86bios_trace_int, 0,
98 "Trace software interrupt handlers");
101 x86bios_set_fault(struct x86emu *emu, uint32_t addr)
105 x86bios_fault_addr = addr;
106 x86bios_fault_cs = emu->x86.R_CS;
107 x86bios_fault_ip = emu->x86.R_IP;
108 x86emu_halt_sys(emu);
112 x86bios_get_pages(uint32_t offset, size_t size)
116 if (offset + size > X86BIOS_MEM_SIZE + X86BIOS_IVT_SIZE)
119 if (offset >= X86BIOS_MEM_SIZE)
120 offset -= X86BIOS_MEM_SIZE;
121 page = x86bios_map[offset / X86BIOS_PAGE_SIZE];
123 return ((void *)(page + offset % X86BIOS_PAGE_SIZE));
129 x86bios_set_pages(vm_offset_t va, vm_paddr_t pa, size_t size)
133 for (i = pa / X86BIOS_PAGE_SIZE, j = 0;
134 j < howmany(size, X86BIOS_PAGE_SIZE); i++, j++)
135 x86bios_map[i] = va + j * X86BIOS_PAGE_SIZE;
139 x86bios_emu_rdb(struct x86emu *emu, uint32_t addr)
143 va = x86bios_get_pages(addr, sizeof(*va));
145 x86bios_set_fault(emu, addr);
151 x86bios_emu_rdw(struct x86emu *emu, uint32_t addr)
155 va = x86bios_get_pages(addr, sizeof(*va));
157 x86bios_set_fault(emu, addr);
159 #ifndef __NO_STRICT_ALIGNMENT
161 return (le16dec(va));
164 return (le16toh(*va));
168 x86bios_emu_rdl(struct x86emu *emu, uint32_t addr)
172 va = x86bios_get_pages(addr, sizeof(*va));
174 x86bios_set_fault(emu, addr);
176 #ifndef __NO_STRICT_ALIGNMENT
178 return (le32dec(va));
181 return (le32toh(*va));
185 x86bios_emu_wrb(struct x86emu *emu, uint32_t addr, uint8_t val)
189 va = x86bios_get_pages(addr, sizeof(*va));
191 x86bios_set_fault(emu, addr);
197 x86bios_emu_wrw(struct x86emu *emu, uint32_t addr, uint16_t val)
201 va = x86bios_get_pages(addr, sizeof(*va));
203 x86bios_set_fault(emu, addr);
205 #ifndef __NO_STRICT_ALIGNMENT
214 x86bios_emu_wrl(struct x86emu *emu, uint32_t addr, uint32_t val)
218 va = x86bios_get_pages(addr, sizeof(*va));
220 x86bios_set_fault(emu, addr);
222 #ifndef __NO_STRICT_ALIGNMENT
231 x86bios_emu_inb(struct x86emu *emu, uint16_t port)
234 if (port == 0xb2) /* APM scratch register */
236 if (port >= 0x80 && port < 0x88) /* POST status register */
243 x86bios_emu_inw(struct x86emu *emu, uint16_t port)
246 if (port >= 0x80 && port < 0x88) /* POST status register */
253 x86bios_emu_inl(struct x86emu *emu, uint16_t port)
256 if (port >= 0x80 && port < 0x88) /* POST status register */
263 x86bios_emu_outb(struct x86emu *emu, uint16_t port, uint8_t val)
266 if (port == 0xb2) /* APM scratch register */
268 if (port >= 0x80 && port < 0x88) /* POST status register */
275 x86bios_emu_outw(struct x86emu *emu, uint16_t port, uint16_t val)
278 if (port >= 0x80 && port < 0x88) /* POST status register */
285 x86bios_emu_outl(struct x86emu *emu, uint16_t port, uint32_t val)
288 if (port >= 0x80 && port < 0x88) /* POST status register */
295 x86bios_alloc(uint32_t *offset, size_t size)
299 if (offset == NULL || size == 0)
302 vaddr = contigmalloc(size, M_DEVBUF, M_NOWAIT, X86BIOS_RAM_BASE,
303 x86bios_rom_phys, X86BIOS_PAGE_SIZE, 0);
305 *offset = vtophys(vaddr);
306 x86bios_set_pages((vm_offset_t)vaddr, *offset, size);
313 x86bios_free(void *addr, size_t size)
317 if (addr == NULL || size == 0)
320 paddr = vtophys(addr);
321 if (paddr < X86BIOS_RAM_BASE || paddr >= x86bios_rom_phys ||
322 paddr % X86BIOS_PAGE_SIZE != 0)
325 bzero(x86bios_map + paddr / X86BIOS_PAGE_SIZE,
326 sizeof(*x86bios_map) * howmany(size, X86BIOS_PAGE_SIZE));
327 contigfree(addr, size, M_DEVBUF);
331 x86bios_init_regs(struct x86regs *regs)
334 bzero(regs, sizeof(*regs));
335 regs->X86BIOS_R_SS = X86BIOS_PHYSTOSEG(x86bios_seg_phys);
336 regs->X86BIOS_R_SP = X86BIOS_PAGE_SIZE - 2;
340 x86bios_call(struct x86regs *regs, uint16_t seg, uint16_t off)
343 if (x86bios_map == NULL)
346 if (x86bios_trace_call)
347 printf("Calling 0x%05x (ax=0x%04x bx=0x%04x "
348 "cx=0x%04x dx=0x%04x es=0x%04x di=0x%04x)\n",
349 (seg << 4) + off, regs->R_AX, regs->R_BX, regs->R_CX,
350 regs->R_DX, regs->R_ES, regs->R_DI);
352 mtx_lock_spin(&x86bios_lock);
353 memcpy(&x86bios_emu.x86, regs, sizeof(*regs));
355 x86emu_exec_call(&x86bios_emu, seg, off);
356 memcpy(regs, &x86bios_emu.x86, sizeof(*regs));
357 mtx_unlock_spin(&x86bios_lock);
359 if (x86bios_trace_call) {
360 printf("Exiting 0x%05x (ax=0x%04x bx=0x%04x "
361 "cx=0x%04x dx=0x%04x es=0x%04x di=0x%04x)\n",
362 (seg << 4) + off, regs->R_AX, regs->R_BX, regs->R_CX,
363 regs->R_DX, regs->R_ES, regs->R_DI);
365 printf("Page fault at 0x%05x from 0x%04x:0x%04x.\n",
366 x86bios_fault_addr, x86bios_fault_cs,
372 x86bios_get_intr(int intno)
376 iv = (uint32_t *)((vm_offset_t)x86bios_ivt + intno * 4);
378 return (le32toh(*iv));
382 x86bios_intr(struct x86regs *regs, int intno)
385 if (intno < 0 || intno > 255)
388 if (x86bios_map == NULL)
391 if (x86bios_trace_int)
392 printf("Calling int 0x%x (ax=0x%04x bx=0x%04x "
393 "cx=0x%04x dx=0x%04x es=0x%04x di=0x%04x)\n",
394 intno, regs->R_AX, regs->R_BX, regs->R_CX,
395 regs->R_DX, regs->R_ES, regs->R_DI);
397 mtx_lock_spin(&x86bios_lock);
398 memcpy(&x86bios_emu.x86, regs, sizeof(*regs));
400 x86emu_exec_intr(&x86bios_emu, intno);
401 memcpy(regs, &x86bios_emu.x86, sizeof(*regs));
402 mtx_unlock_spin(&x86bios_lock);
404 if (x86bios_trace_int) {
405 printf("Exiting int 0x%x (ax=0x%04x bx=0x%04x "
406 "cx=0x%04x dx=0x%04x es=0x%04x di=0x%04x)\n",
407 intno, regs->R_AX, regs->R_BX, regs->R_CX,
408 regs->R_DX, regs->R_ES, regs->R_DI);
410 printf("Page fault at 0x%05x from 0x%04x:0x%04x.\n",
411 x86bios_fault_addr, x86bios_fault_cs,
417 x86bios_offset(uint32_t offset)
420 return (x86bios_get_pages(offset, 1));
424 x86bios_get_orm(uint32_t offset)
428 /* Does the shadow ROM contain BIOS POST code for x86? */
429 p = x86bios_offset(offset);
430 if (p == NULL || p[0] != 0x55 || p[1] != 0xaa ||
431 (p[3] != 0xe9 && p[3] != 0xeb))
438 x86bios_match_device(uint32_t offset, device_t dev)
441 uint16_t device, vendor;
442 uint8_t class, progif, subclass;
444 /* Does the shadow ROM contain BIOS POST code for x86? */
445 p = x86bios_get_orm(offset);
449 /* Does it contain PCI data structure? */
450 p += le16toh(*(uint16_t *)(p + 0x18));
451 if (bcmp(p, "PCIR", 4) != 0 ||
452 le16toh(*(uint16_t *)(p + 0x0a)) < 0x18 || *(p + 0x14) != 0)
455 /* Does it match the vendor, device, and classcode? */
456 vendor = le16toh(*(uint16_t *)(p + 0x04));
457 device = le16toh(*(uint16_t *)(p + 0x06));
458 progif = *(p + 0x0d);
459 subclass = *(p + 0x0e);
461 if (vendor != pci_get_vendor(dev) || device != pci_get_device(dev) ||
462 class != pci_get_class(dev) || subclass != pci_get_subclass(dev) ||
463 progif != pci_get_progif(dev))
469 #if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
476 x86bios_map_mem(void)
479 x86bios_ivt = pmap_mapbios(X86BIOS_IVT_BASE, X86BIOS_IVT_SIZE);
480 if (x86bios_ivt == NULL)
484 /* Probe EBDA via BDA. */
485 x86bios_rom_phys = *(uint16_t *)((vm_offset_t)x86bios_ivt + 0x40e);
486 x86bios_rom_phys = le16toh(x86bios_rom_phys) << 4;
487 if (x86bios_rom_phys != 0 && x86bios_rom_phys < X86BIOS_ROM_BASE &&
488 X86BIOS_ROM_BASE - x86bios_rom_phys <= 128 * 1024)
490 rounddown(x86bios_rom_phys, X86BIOS_PAGE_SIZE);
493 x86bios_rom_phys = X86BIOS_ROM_BASE;
494 x86bios_rom = pmap_mapdev(x86bios_rom_phys, X86BIOS_ROM_SIZE);
495 if (x86bios_rom == NULL) {
496 pmap_unmapdev((vm_offset_t)x86bios_ivt, X86BIOS_IVT_SIZE);
500 /* Change attribute for EBDA. */
501 if (x86bios_rom_phys < X86BIOS_ROM_BASE &&
502 pmap_change_attr((vm_offset_t)x86bios_rom,
503 X86BIOS_ROM_BASE - x86bios_rom_phys, PAT_WRITE_BACK) != 0) {
504 pmap_unmapdev((vm_offset_t)x86bios_ivt, X86BIOS_IVT_SIZE);
505 pmap_unmapdev((vm_offset_t)x86bios_rom, X86BIOS_ROM_SIZE);
510 x86bios_seg = contigmalloc(X86BIOS_SEG_SIZE, M_DEVBUF, M_WAITOK,
511 X86BIOS_RAM_BASE, x86bios_rom_phys, X86BIOS_PAGE_SIZE, 0);
512 x86bios_seg_phys = vtophys(x86bios_seg);
515 printf("x86bios: IVT 0x%06x-0x%06x at %p\n",
516 X86BIOS_IVT_BASE, X86BIOS_IVT_SIZE + X86BIOS_IVT_BASE - 1,
518 printf("x86bios: SSEG 0x%06x-0x%06x at %p\n",
519 (uint32_t)x86bios_seg_phys,
520 X86BIOS_SEG_SIZE + (uint32_t)x86bios_seg_phys - 1,
523 if (x86bios_rom_phys < X86BIOS_ROM_BASE)
524 printf("x86bios: EBDA 0x%06x-0x%06x at %p\n",
525 (uint32_t)x86bios_rom_phys, X86BIOS_ROM_BASE - 1,
528 printf("x86bios: ROM 0x%06x-0x%06x at %p\n",
529 X86BIOS_ROM_BASE, X86BIOS_MEM_SIZE - X86BIOS_SEG_SIZE - 1,
530 (void *)((vm_offset_t)x86bios_rom + X86BIOS_ROM_BASE -
531 (vm_offset_t)x86bios_rom_phys));
540 x86bios_unmap_mem(void)
543 pmap_unmapdev((vm_offset_t)x86bios_ivt, X86BIOS_IVT_SIZE);
544 pmap_unmapdev((vm_offset_t)x86bios_rom, X86BIOS_ROM_SIZE);
545 contigfree(x86bios_seg, X86BIOS_SEG_SIZE, M_DEVBUF);
549 x86bios_init(void *arg __unused)
552 mtx_init(&x86bios_lock, "x86bios lock", NULL, MTX_SPIN);
554 if (x86bios_map_mem() != 0)
557 x86bios_map = malloc(sizeof(*x86bios_map) * X86BIOS_PAGES, M_DEVBUF,
559 x86bios_set_pages((vm_offset_t)x86bios_ivt, X86BIOS_IVT_BASE,
561 x86bios_set_pages((vm_offset_t)x86bios_rom, x86bios_rom_phys,
563 x86bios_set_pages((vm_offset_t)x86bios_seg, x86bios_seg_phys,
566 bzero(&x86bios_emu, sizeof(x86bios_emu));
568 x86bios_emu.emu_rdb = x86bios_emu_rdb;
569 x86bios_emu.emu_rdw = x86bios_emu_rdw;
570 x86bios_emu.emu_rdl = x86bios_emu_rdl;
571 x86bios_emu.emu_wrb = x86bios_emu_wrb;
572 x86bios_emu.emu_wrw = x86bios_emu_wrw;
573 x86bios_emu.emu_wrl = x86bios_emu_wrl;
575 x86bios_emu.emu_inb = x86bios_emu_inb;
576 x86bios_emu.emu_inw = x86bios_emu_inw;
577 x86bios_emu.emu_inl = x86bios_emu_inl;
578 x86bios_emu.emu_outb = x86bios_emu_outb;
579 x86bios_emu.emu_outw = x86bios_emu_outw;
580 x86bios_emu.emu_outl = x86bios_emu_outl;
584 x86bios_uninit(void *arg __unused)
586 vm_offset_t *map = x86bios_map;
588 mtx_lock_spin(&x86bios_lock);
589 if (x86bios_map != NULL) {
590 free(x86bios_map, M_DEVBUF);
593 mtx_unlock_spin(&x86bios_lock);
598 mtx_destroy(&x86bios_lock);
602 x86bios_modevent(module_t mod __unused, int type, void *data __unused)
610 x86bios_uninit(NULL);
619 static moduledata_t x86bios_mod = {
625 DECLARE_MODULE(x86bios, x86bios_mod, SI_SUB_CPU, SI_ORDER_ANY);
626 MODULE_VERSION(x86bios, 1);