xref: /freebsd/sys/i386/i386/bios.c (revision a7cd01df0e5068b7955977172719622f3d55433f)
1d561028dSMike Smith /*-
2d561028dSMike Smith  * Copyright (c) 1997 Michael Smith
3496027bfSMike Smith  * Copyright (c) 1998 Jonathan Lemon
4d561028dSMike Smith  * All rights reserved.
5d561028dSMike Smith  *
6d561028dSMike Smith  * Redistribution and use in source and binary forms, with or without
7d561028dSMike Smith  * modification, are permitted provided that the following conditions
8d561028dSMike Smith  * are met:
9d561028dSMike Smith  * 1. Redistributions of source code must retain the above copyright
10d561028dSMike Smith  *    notice, this list of conditions and the following disclaimer.
11d561028dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
12d561028dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
13d561028dSMike Smith  *    documentation and/or other materials provided with the distribution.
14d561028dSMike Smith  *
15d561028dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16d561028dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17d561028dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18d561028dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19d561028dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20d561028dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21d561028dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22d561028dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23d561028dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24d561028dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25d561028dSMike Smith  * SUCH DAMAGE.
26d561028dSMike Smith  */
27d561028dSMike Smith 
289676a785SDavid E. O'Brien #include <sys/cdefs.h>
299676a785SDavid E. O'Brien __FBSDID("$FreeBSD$");
309676a785SDavid E. O'Brien 
31d561028dSMike Smith /*
32d561028dSMike Smith  * Code for dealing with the BIOS in x86 PC systems.
33d561028dSMike Smith  */
34d561028dSMike Smith 
35586079ccSBruce Evans #include "opt_isa.h"
36586079ccSBruce Evans 
37d561028dSMike Smith #include <sys/param.h>
38d561028dSMike Smith #include <sys/systm.h>
39d561028dSMike Smith #include <sys/kernel.h>
40496027bfSMike Smith #include <sys/malloc.h>
4141ee9f1cSPoul-Henning Kamp #include <sys/module.h>
42bb2b9030SDoug Rabson #include <sys/bus.h>
430bbc8826SJohn Baldwin #include <sys/pcpu.h>
44d561028dSMike Smith #include <vm/vm.h>
45d561028dSMike Smith #include <vm/pmap.h>
46d561028dSMike Smith #include <machine/md_var.h>
47496027bfSMike Smith #include <machine/segments.h>
48496027bfSMike Smith #include <machine/stdarg.h>
49496027bfSMike Smith #include <machine/vmparam.h>
50d561028dSMike Smith #include <machine/pc/bios.h>
51586079ccSBruce Evans #ifdef DEV_ISA
52abe0ccd7SPeter Wemm #include <isa/isavar.h>
536c233a71SPeter Wemm #include <isa/pnpreg.h>
54bb2b9030SDoug Rabson #include <isa/pnpvar.h>
55586079ccSBruce Evans #endif
56d561028dSMike Smith 
57d561028dSMike Smith #define BIOS_START	0xe0000
58d561028dSMike Smith #define BIOS_SIZE	0x20000
59d561028dSMike Smith 
60d561028dSMike Smith /* exported lookup results */
617d224206SJohn Baldwin struct bios32_SDentry		PCIbios;
62bad4ce7dSJohn Baldwin 
63bad4ce7dSJohn Baldwin static struct PnPBIOS_table	*PnPBIOStable;
64d561028dSMike Smith 
657d224206SJohn Baldwin static u_int			bios32_SDCI;
66d561028dSMike Smith 
67d561028dSMike Smith /* start fairly early */
68cb5f885bSMike Smith static void			bios32_init(void *junk);
69d561028dSMike Smith SYSINIT(bios32, SI_SUB_CPU, SI_ORDER_ANY, bios32_init, NULL);
70d561028dSMike Smith 
71d561028dSMike Smith /*
72d561028dSMike Smith  * bios32_init
73d561028dSMike Smith  *
74d561028dSMike Smith  * Locate various bios32 entities.
75d561028dSMike Smith  */
76d561028dSMike Smith static void
77d561028dSMike Smith bios32_init(void *junk)
78d561028dSMike Smith {
79d561028dSMike Smith     u_long			sigaddr;
80d561028dSMike Smith     struct bios32_SDheader	*sdh;
81cb5f885bSMike Smith     struct PnPBIOS_table	*pt;
82d561028dSMike Smith     u_int8_t			ck, *cv;
83d561028dSMike Smith     int				i;
84300451c4SMike Smith     char			*p;
85d561028dSMike Smith 
86d561028dSMike Smith     /*
87300451c4SMike Smith      * BIOS32 Service Directory, PCI BIOS
88d561028dSMike Smith      */
89d561028dSMike Smith 
90d561028dSMike Smith     /* look for the signature */
91d561028dSMike Smith     if ((sigaddr = bios_sigsearch(0, "_32_", 4, 16, 0)) != 0) {
92d561028dSMike Smith 
93d561028dSMike Smith 	/* get a virtual pointer to the structure */
94fc93c1bdSBruce Evans 	sdh = (struct bios32_SDheader *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
95d561028dSMike Smith 	for (cv = (u_int8_t *)sdh, ck = 0, i = 0; i < (sdh->len * 16); i++) {
96d561028dSMike Smith 	    ck += cv[i];
97d561028dSMike Smith 	}
98d561028dSMike Smith 	/* If checksum is OK, enable use of the entrypoint */
99391b5c4aSTakanori Watanabe 	if ((ck == 0) && (BIOS_START <= sdh->entry ) &&
100391b5c4aSTakanori Watanabe 	    (sdh->entry < (BIOS_START + BIOS_SIZE))) {
101496027bfSMike Smith 	    bios32_SDCI = BIOS_PADDRTOVADDR(sdh->entry);
102d561028dSMike Smith 	    if (bootverbose) {
103cb5f885bSMike Smith 		printf("bios32: Found BIOS32 Service Directory header at %p\n", sdh);
104cb5f885bSMike Smith 		printf("bios32: Entry = 0x%x (%x)  Rev = %d  Len = %d\n",
105d561028dSMike Smith 		       sdh->entry, bios32_SDCI, sdh->revision, sdh->len);
106d561028dSMike Smith 	    }
107300451c4SMike Smith 
108300451c4SMike Smith 	    /* Allow user override of PCI BIOS search */
109300451c4SMike Smith 	    if (((p = getenv("machdep.bios.pci")) == NULL) || strcmp(p, "disable")) {
110300451c4SMike Smith 
111d561028dSMike Smith 		/* See if there's a PCI BIOS entrypoint here */
112d561028dSMike Smith 		PCIbios.ident.id = 0x49435024;	/* PCI systems should have this */
113d561028dSMike Smith 		if (!bios32_SDlookup(&PCIbios) && bootverbose)
114300451c4SMike Smith 		    printf("pcibios: PCI BIOS entry at 0x%x+0x%x\n", PCIbios.base, PCIbios.entry);
115300451c4SMike Smith 	    }
116d786139cSMaxime Henrion 	    if (p != NULL)
117d786139cSMaxime Henrion 		    freeenv(p);
118d561028dSMike Smith 	} else {
119cb5f885bSMike Smith 	    printf("bios32: Bad BIOS32 Service Directory\n");
120d561028dSMike Smith 	}
121d561028dSMike Smith     }
122d561028dSMike Smith 
123d561028dSMike Smith     /*
124cb5f885bSMike Smith      * PnP BIOS
125300451c4SMike Smith      *
126300451c4SMike Smith      * Allow user override of PnP BIOS search
127cb5f885bSMike Smith      */
128300451c4SMike Smith     if ((((p = getenv("machdep.bios.pnp")) == NULL) || strcmp(p, "disable")) &&
129300451c4SMike Smith 	((sigaddr = bios_sigsearch(0, "$PnP", 4, 16, 0)) != 0)) {
130d561028dSMike Smith 
131d561028dSMike Smith 	/* get a virtual pointer to the structure */
132cb5f885bSMike Smith 	pt = (struct PnPBIOS_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
133cb5f885bSMike Smith 	for (cv = (u_int8_t *)pt, ck = 0, i = 0; i < pt->len; i++) {
134d561028dSMike Smith 	    ck += cv[i];
135d561028dSMike Smith 	}
136cb5f885bSMike Smith 	/* If checksum is OK, enable use of the entrypoint */
137d561028dSMike Smith 	if (ck == 0) {
138cb5f885bSMike Smith 	    PnPBIOStable = pt;
139d561028dSMike Smith 	    if (bootverbose) {
140cb5f885bSMike Smith 		printf("pnpbios: Found PnP BIOS data at %p\n", pt);
141cb5f885bSMike Smith 		printf("pnpbios: Entry = %x:%x  Rev = %d.%d\n",
142cb5f885bSMike Smith 		       pt->pmentrybase, pt->pmentryoffset, pt->version >> 4, pt->version & 0xf);
143cb5f885bSMike Smith 		if ((pt->control & 0x3) == 0x01)
144cb5f885bSMike Smith 		    printf("pnpbios: Event flag at %x\n", pt->evflagaddr);
145cb5f885bSMike Smith 		if (pt->oemdevid != 0)
146cb5f885bSMike Smith 		    printf("pnpbios: OEM ID %x\n", pt->oemdevid);
147cb5f885bSMike Smith 
148d561028dSMike Smith 	    }
149d561028dSMike Smith 	} else {
150cb5f885bSMike Smith 	    printf("pnpbios: Bad PnP BIOS data checksum\n");
151d561028dSMike Smith 	}
152d561028dSMike Smith     }
153d786139cSMaxime Henrion     if (p != NULL)
154d786139cSMaxime Henrion 	    freeenv(p);
15576cf257bSPoul-Henning Kamp     if (bootverbose) {
15676cf257bSPoul-Henning Kamp 	    /* look for other know signatures */
15776cf257bSPoul-Henning Kamp 	    printf("Other BIOS signatures found:\n");
15876cf257bSPoul-Henning Kamp     }
159d561028dSMike Smith }
160d561028dSMike Smith 
161d561028dSMike Smith /*
162d561028dSMike Smith  * bios32_SDlookup
163d561028dSMike Smith  *
164d561028dSMike Smith  * Query the BIOS32 Service Directory for the service named in (ent),
165d561028dSMike Smith  * returns nonzero if the lookup fails.  The caller must fill in
166d561028dSMike Smith  * (ent->ident), the remainder are populated on a successful lookup.
167d561028dSMike Smith  */
168d561028dSMike Smith int
169d561028dSMike Smith bios32_SDlookup(struct bios32_SDentry *ent)
170d561028dSMike Smith {
171496027bfSMike Smith     struct bios_regs args;
172d561028dSMike Smith 
173496027bfSMike Smith     if (bios32_SDCI == 0)
174496027bfSMike Smith 	return (1);
175d561028dSMike Smith 
176d561028dSMike Smith     args.eax = ent->ident.id;		/* set up arguments */
177d561028dSMike Smith     args.ebx = args.ecx = args.edx = 0;
178496027bfSMike Smith     bios32(&args, bios32_SDCI, GSEL(GCODE_SEL, SEL_KPL));
179d561028dSMike Smith     if ((args.eax & 0xff) == 0) {	/* success? */
180d561028dSMike Smith 	ent->base = args.ebx;
181d561028dSMike Smith 	ent->len = args.ecx;
182d561028dSMike Smith 	ent->entry = args.edx;
183300451c4SMike Smith 	ent->ventry = BIOS_PADDRTOVADDR(ent->base + ent->entry);
184d561028dSMike Smith 	return (0);			/* all OK */
185d561028dSMike Smith     }
186d561028dSMike Smith     return (1);				/* failed */
187d561028dSMike Smith }
188d561028dSMike Smith 
189496027bfSMike Smith 
190d561028dSMike Smith /*
191d561028dSMike Smith  * bios_sigsearch
192d561028dSMike Smith  *
193d561028dSMike Smith  * Search some or all of the BIOS region for a signature string.
194d561028dSMike Smith  *
195d561028dSMike Smith  * (start)	Optional offset returned from this function
196d561028dSMike Smith  *		(for searching for multiple matches), or NULL
197d561028dSMike Smith  *		to start the search from the base of the BIOS.
198d561028dSMike Smith  *		Note that this will be a _physical_ address in
199d561028dSMike Smith  *		the range 0xe0000 - 0xfffff.
200d561028dSMike Smith  * (sig)	is a pointer to the byte(s) of the signature.
201d561028dSMike Smith  * (siglen)	number of bytes in the signature.
202d561028dSMike Smith  * (paralen)	signature paragraph (alignment) size.
203d561028dSMike Smith  * (sigofs)	offset of the signature within the paragraph.
204d561028dSMike Smith  *
205d561028dSMike Smith  * Returns the _physical_ address of the found signature, 0 if the
206d561028dSMike Smith  * signature was not found.
207d561028dSMike Smith  */
208d561028dSMike Smith 
209d561028dSMike Smith u_int32_t
210d561028dSMike Smith bios_sigsearch(u_int32_t start, u_char *sig, int siglen, int paralen, int sigofs)
211d561028dSMike Smith {
212d561028dSMike Smith     u_char	*sp, *end;
213d561028dSMike Smith 
214d561028dSMike Smith     /* compute the starting address */
215d561028dSMike Smith     if ((start >= BIOS_START) && (start <= (BIOS_START + BIOS_SIZE))) {
216d561028dSMike Smith 	sp = (char *)BIOS_PADDRTOVADDR(start);
217d561028dSMike Smith     } else if (start == 0) {
218d561028dSMike Smith 	sp = (char *)BIOS_PADDRTOVADDR(BIOS_START);
219d561028dSMike Smith     } else {
220d561028dSMike Smith 	return 0;				/* bogus start address */
221d561028dSMike Smith     }
222d561028dSMike Smith 
223d561028dSMike Smith     /* compute the end address */
224d561028dSMike Smith     end = (u_char *)BIOS_PADDRTOVADDR(BIOS_START + BIOS_SIZE);
225d561028dSMike Smith 
226d561028dSMike Smith     /* loop searching */
227d561028dSMike Smith     while ((sp + sigofs + siglen) < end) {
228d561028dSMike Smith 
229d561028dSMike Smith 	/* compare here */
230c7a2b294SMike Smith 	if (!bcmp(sp + sigofs, sig, siglen)) {
231d561028dSMike Smith 	    /* convert back to physical address */
232d561028dSMike Smith 	    return((u_int32_t)BIOS_VADDRTOPADDR(sp));
233d561028dSMike Smith 	}
234d561028dSMike Smith 	sp += paralen;
235d561028dSMike Smith     }
236d561028dSMike Smith     return(0);
237d561028dSMike Smith }
238d561028dSMike Smith 
239496027bfSMike Smith /*
240496027bfSMike Smith  * do not staticize, used by bioscall.s
241496027bfSMike Smith  */
242496027bfSMike Smith union {
243496027bfSMike Smith     struct {
244496027bfSMike Smith 	u_short	offset;
245496027bfSMike Smith 	u_short	segment;
246496027bfSMike Smith     } vec16;
247496027bfSMike Smith     struct {
248496027bfSMike Smith 	u_int	offset;
249496027bfSMike Smith 	u_short	segment;
250496027bfSMike Smith     } vec32;
251496027bfSMike Smith } bioscall_vector;			/* bios jump vector */
252496027bfSMike Smith 
253496027bfSMike Smith void
254496027bfSMike Smith set_bios_selectors(struct bios_segments *seg, int flags)
255496027bfSMike Smith {
256496027bfSMike Smith     struct soft_segment_descriptor ssd = {
257496027bfSMike Smith 	0,			/* segment base address (overwritten) */
258496027bfSMike Smith 	0,			/* length (overwritten) */
259496027bfSMike Smith 	SDT_MEMERA,		/* segment type (overwritten) */
260496027bfSMike Smith 	0,			/* priority level */
261496027bfSMike Smith 	1,			/* descriptor present */
262496027bfSMike Smith 	0, 0,
263496027bfSMike Smith 	1,			/* descriptor size (overwritten) */
264496027bfSMike Smith 	0			/* granularity == byte units */
265496027bfSMike Smith     };
2667e42e2f8SJonathan Lemon     union descriptor *p_gdt;
267496027bfSMike Smith 
2687e42e2f8SJonathan Lemon #ifdef SMP
269ef73ae4bSJake Burkholder     p_gdt = &gdt[PCPU_GET(cpuid) * NGDT];
2707e42e2f8SJonathan Lemon #else
2717e42e2f8SJonathan Lemon     p_gdt = gdt;
2727e42e2f8SJonathan Lemon #endif
273496027bfSMike Smith 
274496027bfSMike Smith     ssd.ssd_base = seg->code32.base;
275496027bfSMike Smith     ssd.ssd_limit = seg->code32.limit;
2767e42e2f8SJonathan Lemon     ssdtosd(&ssd, &p_gdt[GBIOSCODE32_SEL].sd);
277496027bfSMike Smith 
278496027bfSMike Smith     ssd.ssd_def32 = 0;
279496027bfSMike Smith     if (flags & BIOSCODE_FLAG) {
280496027bfSMike Smith 	ssd.ssd_base = seg->code16.base;
281496027bfSMike Smith 	ssd.ssd_limit = seg->code16.limit;
2827e42e2f8SJonathan Lemon 	ssdtosd(&ssd, &p_gdt[GBIOSCODE16_SEL].sd);
283496027bfSMike Smith     }
284496027bfSMike Smith 
285496027bfSMike Smith     ssd.ssd_type = SDT_MEMRWA;
286496027bfSMike Smith     if (flags & BIOSDATA_FLAG) {
287496027bfSMike Smith 	ssd.ssd_base = seg->data.base;
288496027bfSMike Smith 	ssd.ssd_limit = seg->data.limit;
2897e42e2f8SJonathan Lemon 	ssdtosd(&ssd, &p_gdt[GBIOSDATA_SEL].sd);
290496027bfSMike Smith     }
291496027bfSMike Smith 
292496027bfSMike Smith     if (flags & BIOSUTIL_FLAG) {
293496027bfSMike Smith 	ssd.ssd_base = seg->util.base;
294496027bfSMike Smith 	ssd.ssd_limit = seg->util.limit;
2957e42e2f8SJonathan Lemon 	ssdtosd(&ssd, &p_gdt[GBIOSUTIL_SEL].sd);
296496027bfSMike Smith     }
297496027bfSMike Smith 
298496027bfSMike Smith     if (flags & BIOSARGS_FLAG) {
299496027bfSMike Smith 	ssd.ssd_base = seg->args.base;
300496027bfSMike Smith 	ssd.ssd_limit = seg->args.limit;
3017e42e2f8SJonathan Lemon 	ssdtosd(&ssd, &p_gdt[GBIOSARGS_SEL].sd);
302496027bfSMike Smith     }
303496027bfSMike Smith }
304496027bfSMike Smith 
305496027bfSMike Smith extern int vm86pa;
30668b7d21aSMike Smith extern void bios16_jmp(void);
307496027bfSMike Smith 
308496027bfSMike Smith /*
309496027bfSMike Smith  * this routine is really greedy with selectors, and uses 5:
310496027bfSMike Smith  *
311496027bfSMike Smith  * 32-bit code selector:	to return to kernel
312496027bfSMike Smith  * 16-bit code selector:	for running code
313496027bfSMike Smith  *        data selector:	for 16-bit data
314496027bfSMike Smith  *        util selector:	extra utility selector
315496027bfSMike Smith  *        args selector:	to handle pointers
316496027bfSMike Smith  *
317496027bfSMike Smith  * the util selector is set from the util16 entry in bios16_args, if a
318496027bfSMike Smith  * "U" specifier is seen.
319496027bfSMike Smith  *
320496027bfSMike Smith  * See <machine/pc/bios.h> for description of format specifiers
321496027bfSMike Smith  */
322496027bfSMike Smith int
323496027bfSMike Smith bios16(struct bios_args *args, char *fmt, ...)
324496027bfSMike Smith {
325496027bfSMike Smith     char	*p, *stack, *stack_top;
326496027bfSMike Smith     va_list 	ap;
327496027bfSMike Smith     int 	flags = BIOSCODE_FLAG | BIOSDATA_FLAG;
328496027bfSMike Smith     u_int 	i, arg_start, arg_end;
329f1b665c8SPeter Wemm     pt_entry_t	*pte;
330f1b665c8SPeter Wemm     pd_entry_t	*ptd;
331496027bfSMike Smith 
332496027bfSMike Smith     arg_start = 0xffffffff;
333496027bfSMike Smith     arg_end = 0;
334496027bfSMike Smith 
335e6a80efcSMike Smith     /*
336e6a80efcSMike Smith      * Some BIOS entrypoints attempt to copy the largest-case
337e6a80efcSMike Smith      * argument frame (in order to generalise handling for
338e6a80efcSMike Smith      * different entry types).  If our argument frame is
339e6a80efcSMike Smith      * smaller than this, the BIOS will reach off the top of
340e6a80efcSMike Smith      * our constructed stack segment.  Pad the top of the stack
341e6a80efcSMike Smith      * with some garbage to avoid this.
342e6a80efcSMike Smith      */
343e6a80efcSMike Smith     stack = (caddr_t)PAGE_SIZE - 32;
344e6a80efcSMike Smith 
345496027bfSMike Smith     va_start(ap, fmt);
346496027bfSMike Smith     for (p = fmt; p && *p; p++) {
347496027bfSMike Smith 	switch (*p) {
348496027bfSMike Smith 	case 'p':			/* 32-bit pointer */
349496027bfSMike Smith 	    i = va_arg(ap, u_int);
350496027bfSMike Smith 	    arg_start = min(arg_start, i);
351f996ef63SMike Smith 	    arg_end = max(arg_end, i);
352496027bfSMike Smith 	    flags |= BIOSARGS_FLAG;
353496027bfSMike Smith 	    stack -= 4;
354496027bfSMike Smith 	    break;
355496027bfSMike Smith 
356496027bfSMike Smith 	case 'i':			/* 32-bit integer */
357496027bfSMike Smith 	    i = va_arg(ap, u_int);
358496027bfSMike Smith 	    stack -= 4;
359496027bfSMike Smith 	    break;
360496027bfSMike Smith 
361496027bfSMike Smith 	case 'U':			/* 16-bit selector */
362496027bfSMike Smith 	    flags |= BIOSUTIL_FLAG;
363496027bfSMike Smith 	    /* FALLTHROUGH */
364496027bfSMike Smith 	case 'D':			/* 16-bit selector */
365496027bfSMike Smith 	case 'C':			/* 16-bit selector */
366cb5f885bSMike Smith 	    stack -= 2;
367cb5f885bSMike Smith 	    break;
368cb5f885bSMike Smith 
36974f168bfSBruce Evans 	case 's':			/* 16-bit integer passed as an int */
37074f168bfSBruce Evans 	    i = va_arg(ap, int);
371496027bfSMike Smith 	    stack -= 2;
372496027bfSMike Smith 	    break;
373496027bfSMike Smith 
374496027bfSMike Smith 	default:
375496027bfSMike Smith 	    return (EINVAL);
376496027bfSMike Smith 	}
377496027bfSMike Smith     }
378496027bfSMike Smith 
379496027bfSMike Smith     if (flags & BIOSARGS_FLAG) {
380496027bfSMike Smith 	if (arg_end - arg_start > ctob(16))
381496027bfSMike Smith 	    return (EACCES);
382496027bfSMike Smith 	args->seg.args.base = arg_start;
383f996ef63SMike Smith 	args->seg.args.limit = 0xffff;
384496027bfSMike Smith     }
385496027bfSMike Smith 
38668b7d21aSMike Smith     args->seg.code32.base = (u_int)&bios16_jmp & PG_FRAME;
387496027bfSMike Smith     args->seg.code32.limit = 0xffff;
388496027bfSMike Smith 
389f1b665c8SPeter Wemm     ptd = (pd_entry_t *)rcr3();
3907ab9b220SJake Burkholder #ifdef PAE
391618b80ddSPoul-Henning Kamp     if (ptd == IdlePDPT)
3927ab9b220SJake Burkholder #else
393618b80ddSPoul-Henning Kamp     if (ptd == IdlePTD)
3947ab9b220SJake Burkholder #endif
395618b80ddSPoul-Henning Kamp     {
396496027bfSMike Smith 	/*
397496027bfSMike Smith 	 * no page table, so create one and install it.
398496027bfSMike Smith 	 */
399a163d034SWarner Losh 	pte = (pt_entry_t *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
4007ab9b220SJake Burkholder 	ptd = (pd_entry_t *)((u_int)IdlePTD + KERNBASE);
4016ccf265bSPeter Wemm 	*pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
402496027bfSMike Smith 	*ptd = vtophys(pte) | PG_RW | PG_V;
403496027bfSMike Smith     } else {
404496027bfSMike Smith 	/*
405496027bfSMike Smith 	 * this is a user-level page table
406496027bfSMike Smith 	 */
407f1b665c8SPeter Wemm 	pte = PTmap;
408496027bfSMike Smith 	*pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
4096ccf265bSPeter Wemm     }
4106ccf265bSPeter Wemm     pmap_invalidate_all(kernel_pmap);	/* XXX insurance for now */
411496027bfSMike Smith 
412496027bfSMike Smith     stack_top = stack;
413496027bfSMike Smith     va_start(ap, fmt);
414496027bfSMike Smith     for (p = fmt; p && *p; p++) {
415496027bfSMike Smith 	switch (*p) {
416496027bfSMike Smith 	case 'p':			/* 32-bit pointer */
417496027bfSMike Smith 	    i = va_arg(ap, u_int);
418496027bfSMike Smith 	    *(u_int *)stack = (i - arg_start) |
419496027bfSMike Smith 		(GSEL(GBIOSARGS_SEL, SEL_KPL) << 16);
420496027bfSMike Smith 	    stack += 4;
421496027bfSMike Smith 	    break;
422496027bfSMike Smith 
423496027bfSMike Smith 	case 'i':			/* 32-bit integer */
424496027bfSMike Smith 	    i = va_arg(ap, u_int);
425496027bfSMike Smith 	    *(u_int *)stack = i;
426496027bfSMike Smith 	    stack += 4;
427496027bfSMike Smith 	    break;
428496027bfSMike Smith 
429496027bfSMike Smith 	case 'U':			/* 16-bit selector */
430496027bfSMike Smith 	    *(u_short *)stack = GSEL(GBIOSUTIL_SEL, SEL_KPL);
431496027bfSMike Smith 	    stack += 2;
432496027bfSMike Smith 	    break;
433496027bfSMike Smith 
434496027bfSMike Smith 	case 'D':			/* 16-bit selector */
435496027bfSMike Smith 	    *(u_short *)stack = GSEL(GBIOSDATA_SEL, SEL_KPL);
436496027bfSMike Smith 	    stack += 2;
437496027bfSMike Smith 	    break;
438496027bfSMike Smith 
439496027bfSMike Smith 	case 'C':			/* 16-bit selector */
440496027bfSMike Smith 	    *(u_short *)stack = GSEL(GBIOSCODE16_SEL, SEL_KPL);
441496027bfSMike Smith 	    stack += 2;
442496027bfSMike Smith 	    break;
443496027bfSMike Smith 
44474f168bfSBruce Evans 	case 's':			/* 16-bit integer passed as an int */
44574f168bfSBruce Evans 	    i = va_arg(ap, int);
446496027bfSMike Smith 	    *(u_short *)stack = i;
447496027bfSMike Smith 	    stack += 2;
448496027bfSMike Smith 	    break;
449496027bfSMike Smith 
450496027bfSMike Smith 	default:
451496027bfSMike Smith 	    return (EINVAL);
452496027bfSMike Smith 	}
453496027bfSMike Smith     }
454496027bfSMike Smith 
455496027bfSMike Smith     set_bios_selectors(&args->seg, flags);
456496027bfSMike Smith     bioscall_vector.vec16.offset = (u_short)args->entry;
457496027bfSMike Smith     bioscall_vector.vec16.segment = GSEL(GBIOSCODE16_SEL, SEL_KPL);
458496027bfSMike Smith 
459496027bfSMike Smith     i = bios16_call(&args->r, stack_top);
460496027bfSMike Smith 
461f1b665c8SPeter Wemm     if (pte == PTmap) {
462496027bfSMike Smith 	*pte = 0;			/* remove entry */
463496027bfSMike Smith 	/*
464496027bfSMike Smith 	 * XXX only needs to be invlpg(0) but that doesn't work on the 386
465496027bfSMike Smith 	 */
466f1b665c8SPeter Wemm 	pmap_invalidate_all(kernel_pmap);
4676ccf265bSPeter Wemm     } else {
4686ccf265bSPeter Wemm 	*ptd = 0;			/* remove page table */
4696ccf265bSPeter Wemm 	/*
4706ccf265bSPeter Wemm 	 * XXX only needs to be invlpg(0) but that doesn't work on the 386
4716ccf265bSPeter Wemm 	 */
4726ccf265bSPeter Wemm 	pmap_invalidate_all(kernel_pmap);
4736ccf265bSPeter Wemm 	free(pte, M_TEMP);		/* ... and free it */
4746ccf265bSPeter Wemm     }
475496027bfSMike Smith     return (i);
476496027bfSMike Smith }
477cb5f885bSMike Smith 
47879005bbdSPoul-Henning Kamp const u_char *
47979005bbdSPoul-Henning Kamp bios_string(u_int from, u_int to, const u_char *string, int len)
48079005bbdSPoul-Henning Kamp {
48179005bbdSPoul-Henning Kamp 	const char *t, *te;
48279005bbdSPoul-Henning Kamp 
48379005bbdSPoul-Henning Kamp 	if (len == 0)
48479005bbdSPoul-Henning Kamp 		len = strlen(string);
48579005bbdSPoul-Henning Kamp 	t = (const char *)(KERNBASE + from);
48679005bbdSPoul-Henning Kamp 	te = (const char *)(KERNBASE + to);
48779005bbdSPoul-Henning Kamp 	for (; t <= te; t++)
48879005bbdSPoul-Henning Kamp 		if (!memcmp(string, t, len))
48979005bbdSPoul-Henning Kamp 			return (t);
49079005bbdSPoul-Henning Kamp 	return (NULL);
49179005bbdSPoul-Henning Kamp }
49279005bbdSPoul-Henning Kamp 
493586079ccSBruce Evans #ifdef DEV_ISA
494cb5f885bSMike Smith /*
495cb5f885bSMike Smith  * PnP BIOS interface; enumerate devices only known to the system
496cb5f885bSMike Smith  * BIOS and save information about them for later use.
497cb5f885bSMike Smith  */
498cb5f885bSMike Smith 
499cb5f885bSMike Smith struct pnp_sysdev
500cb5f885bSMike Smith {
501cb5f885bSMike Smith     u_int16_t	size;
502cb5f885bSMike Smith     u_int8_t	handle;
503cb5f885bSMike Smith     u_int32_t	devid;
504cb5f885bSMike Smith     u_int8_t	type[3];
505cb5f885bSMike Smith     u_int16_t	attrib;
506cb5f885bSMike Smith #define PNPATTR_NODISABLE	(1<<0)	/* can't be disabled */
507cb5f885bSMike Smith #define PNPATTR_NOCONFIG	(1<<1)	/* can't be configured */
508cb5f885bSMike Smith #define PNPATTR_OUTPUT		(1<<2)	/* can be primary output */
509cb5f885bSMike Smith #define PNPATTR_INPUT		(1<<3)	/* can be primary input */
510cb5f885bSMike Smith #define PNPATTR_BOOTABLE	(1<<4)	/* can be booted from */
511cb5f885bSMike Smith #define PNPATTR_DOCK		(1<<5)	/* is a docking station */
512cb5f885bSMike Smith #define PNPATTR_REMOVEABLE	(1<<6)	/* device is removeable */
513c3959391SKazutaka YOKOTA #define PNPATTR_CONFIG_STATIC	(0)
514c3959391SKazutaka YOKOTA #define PNPATTR_CONFIG_DYNAMIC	(1)
515c3959391SKazutaka YOKOTA #define PNPATTR_CONFIG_DYNONLY	(3)
516c3959391SKazutaka YOKOTA #define PNPATTR_CONFIG(a)	(((a) >> 7) & 0x3)
517cb5f885bSMike Smith     /* device-specific data comes here */
518cb5f885bSMike Smith     u_int8_t	devdata[0];
5194f492bfaSAlfred Perlstein } __packed;
520cb5f885bSMike Smith 
521cb5f885bSMike Smith /* We have to cluster arguments within a 64k range for the bios16 call */
522cb5f885bSMike Smith struct pnp_sysdevargs
523cb5f885bSMike Smith {
524cb5f885bSMike Smith     u_int16_t	next;
525cb5f885bSMike Smith     struct pnp_sysdev node;
526cb5f885bSMike Smith };
527cb5f885bSMike Smith 
528cb5f885bSMike Smith /*
529bb2b9030SDoug Rabson  * This function is called after the bus has assigned resource
530bb2b9030SDoug Rabson  * locations for a logical device.
531bb2b9030SDoug Rabson  */
532bb2b9030SDoug Rabson static void
533bb2b9030SDoug Rabson pnpbios_set_config(void *arg, struct isa_config *config, int enable)
534bb2b9030SDoug Rabson {
535bb2b9030SDoug Rabson }
536bb2b9030SDoug Rabson 
537bb2b9030SDoug Rabson /*
538cb5f885bSMike Smith  * Quiz the PnP BIOS, build a list of PNP IDs and resource data.
539cb5f885bSMike Smith  */
540cb5f885bSMike Smith static void
541bb2b9030SDoug Rabson pnpbios_identify(driver_t *driver, device_t parent)
542cb5f885bSMike Smith {
543cb5f885bSMike Smith     struct PnPBIOS_table	*pt = PnPBIOStable;
544cb5f885bSMike Smith     struct bios_args		args;
545cb5f885bSMike Smith     struct pnp_sysdev		*pd;
546cb5f885bSMike Smith     struct pnp_sysdevargs	*pda;
547cb5f885bSMike Smith     u_int16_t			ndevs, bigdev;
548cb5f885bSMike Smith     int				error, currdev;
549cb5f885bSMike Smith     u_int8_t			*devnodebuf, tag;
550cb5f885bSMike Smith     u_int32_t			*devid, *compid;
551cb5f885bSMike Smith     int				idx, left;
552bb2b9030SDoug Rabson     device_t			dev;
553cb5f885bSMike Smith 
554cb5f885bSMike Smith     /* no PnP BIOS information */
555cb5f885bSMike Smith     if (pt == NULL)
556cb5f885bSMike Smith 	return;
557cb5f885bSMike Smith 
558a7cd01dfSJohn Baldwin     /* Check to see if ACPI is already active. */
559a7cd01dfSJohn Baldwin     dev = devclass_get_device(devclass_find("acpi"), 0);
560a7cd01dfSJohn Baldwin     if (dev != NULL && device_is_attached(dev))
561a91d0e1cSMike Smith 	return;
562a91d0e1cSMike Smith 
563a91d0e1cSMike Smith     /* get count of PnP devices */
564cb5f885bSMike Smith     bzero(&args, sizeof(args));
565cb5f885bSMike Smith     args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase);
566cb5f885bSMike Smith     args.seg.code16.limit = 0xffff;		/* XXX ? */
567cb5f885bSMike Smith     args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg);
568cb5f885bSMike Smith     args.seg.data.limit = 0xffff;
569cb5f885bSMike Smith     args.entry = pt->pmentryoffset;
570cb5f885bSMike Smith 
571cb5f885bSMike Smith     if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff))
572cb5f885bSMike Smith 	printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax);
57303c6be5cSMike Smith     ndevs &= 0xff;				/* clear high byte garbage */
574cb5f885bSMike Smith     if (bootverbose)
575cb5f885bSMike Smith 	printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev);
576cb5f885bSMike Smith 
577cb5f885bSMike Smith     devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)),
578cb5f885bSMike Smith 			M_DEVBUF, M_NOWAIT);
579cb5f885bSMike Smith     pda = (struct pnp_sysdevargs *)devnodebuf;
580cb5f885bSMike Smith     pd = &pda->node;
581cb5f885bSMike Smith 
582cb5f885bSMike Smith     for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) {
583cb5f885bSMike Smith 
584cb5f885bSMike Smith 	bzero(pd, bigdev);
585cb5f885bSMike Smith 	pda->next = currdev;
586cb5f885bSMike Smith 	/* get current configuration */
58774f168bfSBruce Evans 	if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, 1))) {
588cb5f885bSMike Smith 	    printf("pnpbios: error %d making BIOS16 call\n", error);
589cb5f885bSMike Smith 	    break;
590cb5f885bSMike Smith 	}
591cb5f885bSMike Smith 	if ((error = (args.r.eax & 0xff))) {
592cb5f885bSMike Smith 	    if (bootverbose)
593cb5f885bSMike Smith 		printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev);
594cb5f885bSMike Smith 	    if (error & 0x80)
595cb5f885bSMike Smith 		break;
596cb5f885bSMike Smith 	}
597cb5f885bSMike Smith 	currdev = pda->next;
598cb5f885bSMike Smith 	if (pd->size < sizeof(struct pnp_sysdev)) {
599cb5f885bSMike Smith 	    printf("pnpbios: bogus system node data, aborting scan\n");
600cb5f885bSMike Smith 	    break;
601cb5f885bSMike Smith 	}
602cb5f885bSMike Smith 
603642ba07aSMike Smith 	/*
60497380242SJohn Baldwin 	 * Ignore PICs so that we don't have to worry about the PICs
60597380242SJohn Baldwin 	 * claiming IRQs to prevent their use.  The PIC drivers
60697380242SJohn Baldwin 	 * already ensure that invalid IRQs are not used.
607642ba07aSMike Smith 	 */
6086eb9d785SMike Smith 	if (!strcmp(pnp_eisaformat(pd->devid), "PNP0000"))	/* ISA PIC */
609642ba07aSMike Smith 	    continue;
6106eb9d785SMike Smith 	if (!strcmp(pnp_eisaformat(pd->devid), "PNP0003"))	/* APIC */
611642ba07aSMike Smith 	    continue;
612642ba07aSMike Smith 
613bb2b9030SDoug Rabson 	/* Add the device and parse its resources */
614bb2b9030SDoug Rabson 	dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
615bb2b9030SDoug Rabson 	isa_set_vendorid(dev, pd->devid);
616bb2b9030SDoug Rabson 	isa_set_logicalid(dev, pd->devid);
617c3959391SKazutaka YOKOTA 	/*
618c3959391SKazutaka YOKOTA 	 * It appears that some PnP BIOS doesn't allow us to re-enable
619c3959391SKazutaka YOKOTA 	 * the embedded system device once it is disabled.  We shall
620c3959391SKazutaka YOKOTA 	 * mark all system device nodes as "cannot be disabled", regardless
621c3959391SKazutaka YOKOTA 	 * of actual settings in the device attribute byte.
622c3959391SKazutaka YOKOTA 	 * XXX
623c3959391SKazutaka YOKOTA 	isa_set_configattr(dev,
624c3959391SKazutaka YOKOTA 	    ((pd->attrib & PNPATTR_NODISABLE) ?  0 : ISACFGATTR_CANDISABLE) |
625c3959391SKazutaka YOKOTA 	    ((!(pd->attrib & PNPATTR_NOCONFIG) &&
626c3959391SKazutaka YOKOTA 		PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
627c3959391SKazutaka YOKOTA 		? ISACFGATTR_DYNAMIC : 0));
628c3959391SKazutaka YOKOTA 	 */
629c3959391SKazutaka YOKOTA 	isa_set_configattr(dev,
630c3959391SKazutaka YOKOTA 	    (!(pd->attrib & PNPATTR_NOCONFIG) &&
631c3959391SKazutaka YOKOTA 		PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
632c3959391SKazutaka YOKOTA 		? ISACFGATTR_DYNAMIC : 0);
633bb2b9030SDoug Rabson 	ISA_SET_CONFIG_CALLBACK(parent, dev, pnpbios_set_config, 0);
634bb2b9030SDoug Rabson 	pnp_parse_resources(dev, &pd->devdata[0],
635c3959391SKazutaka YOKOTA 			    pd->size - sizeof(struct pnp_sysdev), 0);
636bb2b9030SDoug Rabson 	if (!device_get_desc(dev))
637bb2b9030SDoug Rabson 	    device_set_desc_copy(dev, pnp_eisaformat(pd->devid));
638bb2b9030SDoug Rabson 
639cb5f885bSMike Smith 	/* Find device IDs */
640cb5f885bSMike Smith 	devid = &pd->devid;
641cb5f885bSMike Smith 	compid = NULL;
642cb5f885bSMike Smith 
643cb5f885bSMike Smith 	/* look for a compatible device ID too */
644cb5f885bSMike Smith 	left = pd->size - sizeof(struct pnp_sysdev);
645cb5f885bSMike Smith 	idx = 0;
646cb5f885bSMike Smith 	while (idx < left) {
647cb5f885bSMike Smith 	    tag = pd->devdata[idx++];
648cb5f885bSMike Smith 	    if (PNP_RES_TYPE(tag) == 0) {
649cb5f885bSMike Smith 		/* Small resource */
650cb5f885bSMike Smith 		switch (PNP_SRES_NUM(tag)) {
6516c233a71SPeter Wemm 		case PNP_TAG_COMPAT_DEVICE:
652cb5f885bSMike Smith 		    compid = (u_int32_t *)(pd->devdata + idx);
653cb5f885bSMike Smith 		    if (bootverbose)
654cb5f885bSMike Smith 			printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid);
655cb5f885bSMike Smith 		    /* FALLTHROUGH */
6566c233a71SPeter Wemm 		case PNP_TAG_END:
657cb5f885bSMike Smith 		    idx = left;
658cb5f885bSMike Smith 		    break;
659cb5f885bSMike Smith 		default:
660cb5f885bSMike Smith 		    idx += PNP_SRES_LEN(tag);
661cb5f885bSMike Smith 		    break;
662cb5f885bSMike Smith 		}
663cb5f885bSMike Smith 	    } else
664cb5f885bSMike Smith 		/* Large resource, skip it */
665cb5f885bSMike Smith 		idx += *(u_int16_t *)(pd->devdata + idx) + 2;
666cb5f885bSMike Smith 	}
667cb5f885bSMike Smith 	if (bootverbose) {
668cb5f885bSMike Smith 	    printf("pnpbios: handle %d device ID %s (%08x)",
669bb2b9030SDoug Rabson 		   pd->handle, pnp_eisaformat(*devid), *devid);
670cb5f885bSMike Smith 	    if (compid != NULL)
671cb5f885bSMike Smith 		printf(" compat ID %s (%08x)",
672bb2b9030SDoug Rabson 		       pnp_eisaformat(*compid), *compid);
673cb5f885bSMike Smith 	    printf("\n");
674cb5f885bSMike Smith 	}
675cb5f885bSMike Smith     }
676cb5f885bSMike Smith }
677cb5f885bSMike Smith 
678bb2b9030SDoug Rabson static device_method_t pnpbios_methods[] = {
679bb2b9030SDoug Rabson 	/* Device interface */
680bb2b9030SDoug Rabson 	DEVMETHOD(device_identify,	pnpbios_identify),
681cb5f885bSMike Smith 
682bb2b9030SDoug Rabson 	{ 0, 0 }
683bb2b9030SDoug Rabson };
684bb2b9030SDoug Rabson 
685bb2b9030SDoug Rabson static driver_t pnpbios_driver = {
686bb2b9030SDoug Rabson 	"pnpbios",
687bb2b9030SDoug Rabson 	pnpbios_methods,
688bb2b9030SDoug Rabson 	1,			/* no softc */
689bb2b9030SDoug Rabson };
690bb2b9030SDoug Rabson 
691bb2b9030SDoug Rabson static devclass_t pnpbios_devclass;
692bb2b9030SDoug Rabson 
693bb2b9030SDoug Rabson DRIVER_MODULE(pnpbios, isa, pnpbios_driver, pnpbios_devclass, 0, 0);
694586079ccSBruce Evans #endif /* DEV_ISA */
695