blob: 1ccf5eddea1e7865bc8cd6cb304b8733653e428a [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001This code implements an X86 legacy bios. It is intended to be
2compiled using standard gnu tools (eg, gas and gcc).
3
4To build, one should be able to run "make" in the main directory. The
Kevin O'Connor59fead62008-05-10 15:49:20 -04005resulting file "out/bios.bin" contains the processed bios image.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05006
Kevin O'Connor838f08f2008-03-30 11:07:42 -04007The build requires gcc v4.1 or later. Some buggy versions of gcc have
8issues with the '-combine' compiler option - in particular, recent
9versions of Ubuntu are affected. One can use "make AVOIDCOMBINE=1" to
10get around this.
11
12
13Testing of images:
14
15To test the bios under bochs, one will need to instruct bochs to use
16the new bios image. Use the 'romimage' option - for example:
17
Kevin O'Connor59fead62008-05-10 15:49:20 -040018bochs -q 'floppya: 1_44=myfdimage.img' 'romimage: file=out/bios.bin'
Kevin O'Connor838f08f2008-03-30 11:07:42 -040019
20To test under qemu, one will need to create a directory with all the
21bios images and then overwrite the main bios image. For example:
22
23cp /usr/share/qemu/*.bin mybiosdir/
Kevin O'Connor59fead62008-05-10 15:49:20 -040024cp out/bios.bin mybiosdir/
Kevin O'Connor838f08f2008-03-30 11:07:42 -040025
26Once this is setup, one can instruct qemu to use the newly created
27directory for rom images. For example:
28
29qemu -L mybiosdir/ -fda myfdimage.img
30
31
32The following payloads have been tested:
33
34Freedos - see http://www.freedos.org/ . Useful tests include: booting
35from installation cdrom, installing to hard drive and floppy, making
36sure hard drive and floppy boots then work. It is also useful to take
37the bootable floppy and hard-drive images, write them to an el-torito
38bootable cdrom using the Linux mkisofs utility, and then boot those
39cdrom images.
40
41Linux - useful hard drive image available from
42http://fabrice.bellard.free.fr/qemu/linux-0.2.img.bz2 . It is also
43useful to test standard distribution bootup and live cdroms.
44
45NetBSD - useful hard drive image available from
46http://nopid.free.fr/small.ffs.bz2 . It is also useful to test
47standard distribution installation cdroms.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050048
49
50Overview of files:
51
Kevin O'Connor838f08f2008-03-30 11:07:42 -040052The src/ directory contains the bios source code. Several of the
53files are compiled twice - once for 16bit mode and once for 32bit
Kevin O'Connor0bb2a442008-04-01 21:09:05 -040054mode. (The gcc compile option '-fwhole-program' is used to remove
55code that is not needed for a particular mode.)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050056
57The tools/ directory contains helper utilities for manipulating and
58building the final rom.
59
60The out/ directory is created by the build process - it contains all
61temporary and final files.
62
63
64Build overview:
65
Kevin O'Connor0afee522009-02-05 20:32:41 -050066The 16bit code is compiled via gcc to assembler (file out/ccode.16.s).
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050067The gcc "-fwhole-program" option is used to optimize the process so
Kevin O'Connor0bb2a442008-04-01 21:09:05 -040068that gcc can efficiently compile and discard unneeded code. (In the
69code, one can use the macros 'VISIBLE16' and 'VISIBLE32' to instruct a
70symbol to be outputted in 16bit and 32bit mode respectively.)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050071
72This resulting assembler code is pulled into romlayout.S. The gas
73option ".code16gcc" is used prior to including the gcc generated
Kevin O'Connor838f08f2008-03-30 11:07:42 -040074assembler - this option enables gcc to generate valid 16 bit code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050075
Kevin O'Connor838f08f2008-03-30 11:07:42 -040076The post code (post.c) is entered, via the function _start(), in 32bit
77mode. The 16bit post vector (in romlayout.S) transitions the cpu into
7832 bit mode before calling the post.c code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050079
Kevin O'Connor838f08f2008-03-30 11:07:42 -040080In the last step of compilation, the 32 bit code is merged into the 16
81bit code so that one binary file contains both. Currently, both 16bit
82and 32bit code will be located in the 64K block at segment 0xf000.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050083
84
85GCC 16 bit limitations:
86
87Although the 16bit code is compiled with gcc, developers need to be
88aware of the environment. In particular, global variables _must_ be
89treated specially.
90
91The code has full access to stack variables and general purpose
92registers. The entry code in romlayout.S will push the original
93registers on the stack before calling the C code and then pop them off
94(including any required changes) before returning from the interrupt.
95Changes to CS, DS, and ES segment registers in C code is also safe.
96Changes to other segment registers (SS, FS, GS) need to be restored
97manually.
98
99Stack variables (and pointers to stack variables) work as they
100normally do in standard C code.
101
102However, variables stored outside the stack need to be accessed via
Kevin O'Connor838f08f2008-03-30 11:07:42 -0400103the GET_VAR and SET_VAR macros (or one of the helper macros described
104below). This is due to the 16bit segment nature of the X86 cpu when
105it is in "real mode". The C entry code will set DS and SS to point to
106the stack segment. Variables not on the stack need to be accessed via
Kevin O'Connor0afee522009-02-05 20:32:41 -0500107an explicit segment register. Any other access requires altering one
108of the other segment registers (usually ES) and then accessing the
109variable via that segment register.
Kevin O'Connor838f08f2008-03-30 11:07:42 -0400110
111There are three low-level ways to access a remote variable:
Kevin O'Connor0afee522009-02-05 20:32:41 -0500112GET/SET_VAR, GET/SET_FARVAR, and GET/SET_FLATPTR. The first set takes
Kevin O'Connor838f08f2008-03-30 11:07:42 -0400113an explicit segment descriptor (eg, "CS") and offset. The second set
Kevin O'Connor0afee522009-02-05 20:32:41 -0500114will take a segment id and offset, set ES to the segment id, and then
Kevin O'Connor838f08f2008-03-30 11:07:42 -0400115make the access via the ES segment. The last method is similar to the
Kevin O'Connor0afee522009-02-05 20:32:41 -0500116second, except it takes a pointer that would be valid in 32-bit flat
117mode instead of a segment/offset pair.
Kevin O'Connor838f08f2008-03-30 11:07:42 -0400118
Kevin O'Connor0afee522009-02-05 20:32:41 -0500119Most BIOS variables are stored in global variables, the "BDA", or
120"EBDA" memory areas. Because this is common, three sets of helper
121macros (GET/SET_GLOBAL, GET/SET_BDA, and GET/SET_EBDA) are available
122to simplify these accesses.
123
124Global variables defined in the C code can be read in 16bit mode if
125the variable declaration is marked with VAR16 or VAR16_32. The
126GET_GLOBAL macro will then allow read access to the variable. Global
127variables are stored in the 0xf000 segment, and their values are
128persistent across soft resets. Because the f-segment is marked
129read-only during run-time, the 16bit code is not permitted to change
130the value of 16bit variables (use of the SET_GLOBAL macro from 16bit
131mode will cause a link error). Code running in 32bit mode can not
132access variables with VAR16, but can access variables marked with
133VAR16_32 or with no marking at all. The 32bit code can use the
134GET/SET_GLOBAL macros, but they are not required.
Kevin O'Connor838f08f2008-03-30 11:07:42 -0400135
136
137GCC 16 bit stack limitations:
138
139Another limitation of gcc is its use of 32-bit temporaries. Gcc will
140allocate 32-bits of space for every variable - even if that variable
141is only defined as a 'u8' or 'u16'. If one is not careful, using too
142much stack space can break old DOS applications.
143
144There does not appear to be explicit documentation on the minimum
145stack space available for bios calls. However, Freedos has been
Kevin O'Connor0bb2a442008-04-01 21:09:05 -0400146observed to call into the bios with less than 150 bytes available.
Kevin O'Connor838f08f2008-03-30 11:07:42 -0400147
148Note that the post code and boot code (irq 18/19) do not have a stack
Kevin O'Connor0afee522009-02-05 20:32:41 -0500149limitation because the entry points for these functions transition the
150cpu to 32bit mode and reset the stack to a known state. Only the
151general purpose 16-bit service entry points are affected.
Kevin O'Connor838f08f2008-03-30 11:07:42 -0400152
153There are some ways to reduce stack usage: making sure functions are
154tail-recursive often helps, reducing the number of parameters passed
155to functions often helps, sometimes reordering variable declarations
156helps, inlining of functions can sometimes help, and passing of packed
Kevin O'Connor0afee522009-02-05 20:32:41 -0500157structures can also help. It is also possible to transition to/from
158an extra stack stored in the EBDA using the stack_hop helper function.
Kevin O'Connor838f08f2008-03-30 11:07:42 -0400159
Kevin O'Connor0bb2a442008-04-01 21:09:05 -0400160Some useful stats: the overhead for the entry to a bios handler that
161takes a 'struct bregs' is 38 bytes of stack space (6 bytes from
162interrupt insn, 28 bytes to store registers, and 4 bytes for call
163insn). An entry to an ISR handler without args takes 30 bytes (6 + 20
164+ 4).
165
Kevin O'Connor838f08f2008-03-30 11:07:42 -0400166
167Debugging the bios:
168
169The bios will output information messages to a special debug port.
170Under qemu, one can view these messages by enabling the '#define
171DEBUG_BIOS' definition in 'qemu/hw/pc.c'. Once this is done (and qemu
172is recompiled), one should see status messages on the console.
173
174The gdb-server mechanism of qemu is also useful. One can use gdb with
175qemu to debug system images. To use this, add '-s -S' to the qemu
176command line. For example:
177
178qemu -L mybiosdir/ -fda myfdimage.img -s -S
179
180Then, in another session, run gdb with either out/rom16.o (to debug
181bios 16bit code) or out/rom32.o (to debug bios 32bit code). For
182example:
183
184gdb out/rom16.o
185
186Once in gdb, use the command "target remote localhost:1234" to have
187gdb connect to qemu. See the qemu documentation for more information
188on using gdb and qemu in this mode. Note that gdb seems to get
189breakpoints confused when the cpu is in 16-bit real mode. This makes
190stepping through the program difficult (though 'step instruction'
191still works). Also, one may need to set 16bit break points at both
192the cpu address and memory address (eg, break *0x1234 ; break
193*0xf1234).