]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/compat/x86bios/x86bios.c
MFC r286886: Fixing typo as well as improving readability of a few comments.
[FreeBSD/stable/8.git] / sys / compat / x86bios / x86bios.c
1 /*-
2  * Copyright (c) 2009 Alex Keda <admin@lissyara.su>
3  * Copyright (c) 2009-2010 Jung-uk Kim <jkim@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_x86bios.h"
32
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/sysctl.h>
42
43 #include <contrib/x86emu/x86emu.h>
44 #include <contrib/x86emu/x86emu_regs.h>
45 #include <compat/x86bios/x86bios.h>
46
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49
50 #include <machine/cpufunc.h>
51
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54
55 #define X86BIOS_PAGE_SIZE       0x00001000      /* 4K */
56
57 #define X86BIOS_IVT_SIZE        0x00000500      /* 1K + 256 (BDA) */
58 #define X86BIOS_MEM_SIZE        0x00100000      /* 1M */
59
60 #define X86BIOS_IVT_BASE        0x00000000
61 #define X86BIOS_RAM_BASE        0x00001000
62 #define X86BIOS_ROM_BASE        0x000a0000
63
64 #define X86BIOS_ROM_SIZE        (X86BIOS_MEM_SIZE - (uint32_t)x86bios_rom_phys)
65 #define X86BIOS_SEG_SIZE        X86BIOS_PAGE_SIZE
66
67 #define X86BIOS_PAGES           (X86BIOS_MEM_SIZE / X86BIOS_PAGE_SIZE)
68
69 #define X86BIOS_R_SS            _pad2
70 #define X86BIOS_R_SP            _pad3.I16_reg.x_reg
71
72 static struct x86emu x86bios_emu;
73
74 static struct mtx x86bios_lock;
75
76 static void *x86bios_ivt;
77 static void *x86bios_rom;
78 static void *x86bios_seg;
79
80 static vm_offset_t *x86bios_map;
81
82 static vm_paddr_t x86bios_rom_phys;
83 static vm_paddr_t x86bios_seg_phys;
84
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;
89
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");
99
100 static void
101 x86bios_set_fault(struct x86emu *emu, uint32_t addr)
102 {
103
104         x86bios_fault = 1;
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);
109 }
110
111 static void *
112 x86bios_get_pages(uint32_t offset, size_t size)
113 {
114         vm_offset_t page;
115
116         if (offset + size > X86BIOS_MEM_SIZE + X86BIOS_IVT_SIZE)
117                 return (NULL);
118
119         if (offset >= X86BIOS_MEM_SIZE)
120                 offset -= X86BIOS_MEM_SIZE;
121         page = x86bios_map[offset / X86BIOS_PAGE_SIZE];
122         if (page != 0)
123                 return ((void *)(page + offset % X86BIOS_PAGE_SIZE));
124
125         return (NULL);
126 }
127
128 static void
129 x86bios_set_pages(vm_offset_t va, vm_paddr_t pa, size_t size)
130 {
131         int i, j;
132
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;
136 }
137
138 static uint8_t
139 x86bios_emu_rdb(struct x86emu *emu, uint32_t addr)
140 {
141         uint8_t *va;
142
143         va = x86bios_get_pages(addr, sizeof(*va));
144         if (va == NULL)
145                 x86bios_set_fault(emu, addr);
146
147         return (*va);
148 }
149
150 static uint16_t
151 x86bios_emu_rdw(struct x86emu *emu, uint32_t addr)
152 {
153         uint16_t *va;
154
155         va = x86bios_get_pages(addr, sizeof(*va));
156         if (va == NULL)
157                 x86bios_set_fault(emu, addr);
158
159 #ifndef __NO_STRICT_ALIGNMENT
160         if ((addr & 1) != 0)
161                 return (le16dec(va));
162         else
163 #endif
164         return (le16toh(*va));
165 }
166
167 static uint32_t
168 x86bios_emu_rdl(struct x86emu *emu, uint32_t addr)
169 {
170         uint32_t *va;
171
172         va = x86bios_get_pages(addr, sizeof(*va));
173         if (va == NULL)
174                 x86bios_set_fault(emu, addr);
175
176 #ifndef __NO_STRICT_ALIGNMENT
177         if ((addr & 3) != 0)
178                 return (le32dec(va));
179         else
180 #endif
181         return (le32toh(*va));
182 }
183
184 static void
185 x86bios_emu_wrb(struct x86emu *emu, uint32_t addr, uint8_t val)
186 {
187         uint8_t *va;
188
189         va = x86bios_get_pages(addr, sizeof(*va));
190         if (va == NULL)
191                 x86bios_set_fault(emu, addr);
192
193         *va = val;
194 }
195
196 static void
197 x86bios_emu_wrw(struct x86emu *emu, uint32_t addr, uint16_t val)
198 {
199         uint16_t *va;
200
201         va = x86bios_get_pages(addr, sizeof(*va));
202         if (va == NULL)
203                 x86bios_set_fault(emu, addr);
204
205 #ifndef __NO_STRICT_ALIGNMENT
206         if ((addr & 1) != 0)
207                 le16enc(va, val);
208         else
209 #endif
210         *va = htole16(val);
211 }
212
213 static void
214 x86bios_emu_wrl(struct x86emu *emu, uint32_t addr, uint32_t val)
215 {
216         uint32_t *va;
217
218         va = x86bios_get_pages(addr, sizeof(*va));
219         if (va == NULL)
220                 x86bios_set_fault(emu, addr);
221
222 #ifndef __NO_STRICT_ALIGNMENT
223         if ((addr & 3) != 0)
224                 le32enc(va, val);
225         else
226 #endif
227         *va = htole32(val);
228 }
229
230 static uint8_t
231 x86bios_emu_inb(struct x86emu *emu, uint16_t port)
232 {
233
234         if (port == 0xb2) /* APM scratch register */
235                 return (0);
236         if (port >= 0x80 && port < 0x88) /* POST status register */
237                 return (0);
238
239         return (inb(port));
240 }
241
242 static uint16_t
243 x86bios_emu_inw(struct x86emu *emu, uint16_t port)
244 {
245
246         if (port >= 0x80 && port < 0x88) /* POST status register */
247                 return (0);
248
249         return (inw(port));
250 }
251
252 static uint32_t
253 x86bios_emu_inl(struct x86emu *emu, uint16_t port)
254 {
255
256         if (port >= 0x80 && port < 0x88) /* POST status register */
257                 return (0);
258
259         return (inl(port));
260 }
261
262 static void
263 x86bios_emu_outb(struct x86emu *emu, uint16_t port, uint8_t val)
264 {
265
266         if (port == 0xb2) /* APM scratch register */
267                 return;
268         if (port >= 0x80 && port < 0x88) /* POST status register */
269                 return;
270
271         outb(port, val);
272 }
273
274 static void
275 x86bios_emu_outw(struct x86emu *emu, uint16_t port, uint16_t val)
276 {
277
278         if (port >= 0x80 && port < 0x88) /* POST status register */
279                 return;
280
281         outw(port, val);
282 }
283
284 static void
285 x86bios_emu_outl(struct x86emu *emu, uint16_t port, uint32_t val)
286 {
287
288         if (port >= 0x80 && port < 0x88) /* POST status register */
289                 return;
290
291         outl(port, val);
292 }
293
294 void *
295 x86bios_alloc(uint32_t *offset, size_t size)
296 {
297         void *vaddr;
298
299         if (offset == NULL || size == 0)
300                 return (NULL);
301
302         vaddr = contigmalloc(size, M_DEVBUF, M_NOWAIT, X86BIOS_RAM_BASE,
303             x86bios_rom_phys, X86BIOS_PAGE_SIZE, 0);
304         if (vaddr != NULL) {
305                 *offset = vtophys(vaddr);
306                 x86bios_set_pages((vm_offset_t)vaddr, *offset, size);
307         }
308
309         return (vaddr);
310 }
311
312 void
313 x86bios_free(void *addr, size_t size)
314 {
315         vm_paddr_t paddr;
316
317         if (addr == NULL || size == 0)
318                 return;
319
320         paddr = vtophys(addr);
321         if (paddr < X86BIOS_RAM_BASE || paddr >= x86bios_rom_phys ||
322             paddr % X86BIOS_PAGE_SIZE != 0)
323                 return;
324
325         bzero(x86bios_map + paddr / X86BIOS_PAGE_SIZE,
326             sizeof(*x86bios_map) * howmany(size, X86BIOS_PAGE_SIZE));
327         contigfree(addr, size, M_DEVBUF);
328 }
329
330 void
331 x86bios_init_regs(struct x86regs *regs)
332 {
333
334         bzero(regs, sizeof(*regs));
335         regs->X86BIOS_R_SS = X86BIOS_PHYSTOSEG(x86bios_seg_phys);
336         regs->X86BIOS_R_SP = X86BIOS_PAGE_SIZE - 2;
337 }
338
339 void
340 x86bios_call(struct x86regs *regs, uint16_t seg, uint16_t off)
341 {
342
343         if (x86bios_map == NULL)
344                 return;
345
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);
351
352         mtx_lock_spin(&x86bios_lock);
353         memcpy(&x86bios_emu.x86, regs, sizeof(*regs));
354         x86bios_fault = 0;
355         x86emu_exec_call(&x86bios_emu, seg, off);
356         memcpy(regs, &x86bios_emu.x86, sizeof(*regs));
357         mtx_unlock_spin(&x86bios_lock);
358
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);
364                 if (x86bios_fault)
365                         printf("Page fault at 0x%05x from 0x%04x:0x%04x.\n",
366                             x86bios_fault_addr, x86bios_fault_cs,
367                             x86bios_fault_ip);
368         }
369 }
370
371 uint32_t
372 x86bios_get_intr(int intno)
373 {
374         uint32_t *iv;
375
376         iv = (uint32_t *)((vm_offset_t)x86bios_ivt + intno * 4);
377
378         return (le32toh(*iv));
379 }
380
381 void
382 x86bios_intr(struct x86regs *regs, int intno)
383 {
384
385         if (intno < 0 || intno > 255)
386                 return;
387
388         if (x86bios_map == NULL)
389                 return;
390
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);
396
397         mtx_lock_spin(&x86bios_lock);
398         memcpy(&x86bios_emu.x86, regs, sizeof(*regs));
399         x86bios_fault = 0;
400         x86emu_exec_intr(&x86bios_emu, intno);
401         memcpy(regs, &x86bios_emu.x86, sizeof(*regs));
402         mtx_unlock_spin(&x86bios_lock);
403
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);
409                 if (x86bios_fault)
410                         printf("Page fault at 0x%05x from 0x%04x:0x%04x.\n",
411                             x86bios_fault_addr, x86bios_fault_cs,
412                             x86bios_fault_ip);
413         }
414 }
415
416 void *
417 x86bios_offset(uint32_t offset)
418 {
419
420         return (x86bios_get_pages(offset, 1));
421 }
422
423 void *
424 x86bios_get_orm(uint32_t offset)
425 {
426         uint8_t *p;
427
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))
432                 return (NULL);
433
434         return (p);
435 }
436
437 int
438 x86bios_match_device(uint32_t offset, device_t dev)
439 {
440         uint8_t *p;
441         uint16_t device, vendor;
442         uint8_t class, progif, subclass;
443
444         /* Does the shadow ROM contain BIOS POST code for x86? */
445         p = x86bios_get_orm(offset);
446         if (p == NULL)
447                 return (0);
448
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)
453                 return (0);
454
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);
460         class = *(p + 0x0f);
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))
464                 return (0);
465
466         return (1);
467 }
468
469 #if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
470 #define PROBE_EBDA      1
471 #else
472 #define PROBE_EBDA      0
473 #endif
474
475 static __inline int
476 x86bios_map_mem(void)
477 {
478
479         x86bios_ivt = pmap_mapbios(X86BIOS_IVT_BASE, X86BIOS_IVT_SIZE);
480         if (x86bios_ivt == NULL)
481                 return (1);
482
483 #if PROBE_EBDA
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)
489                 x86bios_rom_phys =
490                     rounddown(x86bios_rom_phys, X86BIOS_PAGE_SIZE);
491         else
492 #endif
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);
497                 return (1);
498         }
499 #if PROBE_EBDA
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);
506                 return (1);
507         }
508 #endif
509
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);
513
514         if (bootverbose) {
515                 printf("x86bios:   IVT 0x%06x-0x%06x at %p\n",
516                     X86BIOS_IVT_BASE, X86BIOS_IVT_SIZE + X86BIOS_IVT_BASE - 1,
517                     x86bios_ivt);
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,
521                     x86bios_seg);
522 #if PROBE_EBDA
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,
526                             x86bios_rom);
527 #endif
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));
532         }
533
534         return (0);
535 }
536
537 #undef PROBE_EBDA
538
539 static __inline void
540 x86bios_unmap_mem(void)
541 {
542
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);
546 }
547
548 static void
549 x86bios_init(void *arg __unused)
550 {
551
552         mtx_init(&x86bios_lock, "x86bios lock", NULL, MTX_SPIN);
553
554         if (x86bios_map_mem() != 0)
555                 return;
556
557         x86bios_map = malloc(sizeof(*x86bios_map) * X86BIOS_PAGES, M_DEVBUF,
558             M_WAITOK | M_ZERO);
559         x86bios_set_pages((vm_offset_t)x86bios_ivt, X86BIOS_IVT_BASE,
560             X86BIOS_IVT_SIZE);
561         x86bios_set_pages((vm_offset_t)x86bios_rom, x86bios_rom_phys,
562             X86BIOS_ROM_SIZE);
563         x86bios_set_pages((vm_offset_t)x86bios_seg, x86bios_seg_phys,
564             X86BIOS_SEG_SIZE);
565
566         bzero(&x86bios_emu, sizeof(x86bios_emu));
567
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;
574
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;
581 }
582
583 static void
584 x86bios_uninit(void *arg __unused)
585 {
586         vm_offset_t *map = x86bios_map;
587
588         mtx_lock_spin(&x86bios_lock);
589         if (x86bios_map != NULL) {
590                 free(x86bios_map, M_DEVBUF);
591                 x86bios_map = NULL;
592         }
593         mtx_unlock_spin(&x86bios_lock);
594
595         if (map != NULL)
596                 x86bios_unmap_mem();
597
598         mtx_destroy(&x86bios_lock);
599 }
600
601 static int
602 x86bios_modevent(module_t mod __unused, int type, void *data __unused)
603 {
604
605         switch (type) {
606         case MOD_LOAD:
607                 x86bios_init(NULL);
608                 break;
609         case MOD_UNLOAD:
610                 x86bios_uninit(NULL);
611                 break;
612         default:
613                 return (ENOTSUP);
614         }
615
616         return (0);
617 }
618
619 static moduledata_t x86bios_mod = {
620         "x86bios",
621         x86bios_modevent,
622         NULL,
623 };
624
625 DECLARE_MODULE(x86bios, x86bios_mod, SI_SUB_CPU, SI_ORDER_ANY);
626 MODULE_VERSION(x86bios, 1);