From: Matthew Dillon Date: Mon, 19 Jul 2004 23:30:38 +0000 (+0000) Subject: MAJOR BOOT CODE REVAMP / 30 hour+ hacking session (50 if you include the X-Git-Tag: v2.0.1~10758 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/cacaceecf77dc39131bf76571e5acbff01350b2a MAJOR BOOT CODE REVAMP / 30 hour+ hacking session (50 if you include the attempt I made last month). * Document the code wherever possible. The previous code had lots of basically useless comments. * Consolidate constants and calculations into a single header file. The previous code separately defined the constants, did not document the relationships (which is why it took 30+ hours to fix), and often used unrelated numerical optimizations to get from one constant def to another. * Most address ORG values can now be modified in one place (bootasm.h) and the boot code will actually still work. Note, however, that many ORG values must still be < 0x10000 due to the lack of segment register use in 16 bit code. This can be fixed with additional work, but not in this commit. * Provide two alternative address ORG relocations for experimentation purposes. * Fix what I believe to be a bug in boot2/boot2.c. Boot2.c was accessing the drive/slice data stored by boot1 using the base-offset-0 linear address instead of the btx-client-user address. Additionally, boot1 was storing the data at a location that the btx client could not access. (see MEM_BTX_USR_ARG) * Fix a heap/stack collision in the loader. The loader was setting the end of the heap to the end of BIOS memory without taking into account the fact that its stack was likely growing downward starting at the end of BIOS memory. * Fix a heap calculation error in the loader. The loader was using the physical address representation of the end of BIOS memory instead of the virtual address representatino, which can result in the loader pushing into BIOS memory. --- diff --git a/sys/boot/i386/boot0/Makefile b/sys/boot/i386/boot0/Makefile index c5a098a936..ff4427095b 100644 --- a/sys/boot/i386/boot0/Makefile +++ b/sys/boot/i386/boot0/Makefile @@ -1,5 +1,5 @@ # $FreeBSD: src/sys/boot/i386/boot0/Makefile,v 1.17 2002/09/17 01:48:54 peter Exp $ -# $DragonFly: src/sys/boot/i386/boot0/Attic/Makefile,v 1.4 2004/07/18 23:39:59 dillon Exp $ +# $DragonFly: src/sys/boot/i386/boot0/Attic/Makefile,v 1.5 2004/07/19 23:30:31 dillon Exp $ PROG= boot0 NOMAN= @@ -7,6 +7,8 @@ STRIP= BINDIR?= /boot BINMODE= 444 +.PATH: ${.CURDIR}/.. + # The default set of flags compiled into boot0. This enables update (writing # the modified boot0 back to disk after running so that the selection made is # saved), packet mode (detect and use the BIOS EDD extensions if we try to @@ -18,18 +20,18 @@ BOOT_BOOT0_FLAGS?= 0xf # 0xb6 (182d) corresponds to 10 seconds. BOOT_BOOT0_TICKS?= 0xb6 -# The base address that we the boot0 code to to run it. Don't change this -# unless you are glutton for punishment. -BOOT_BOOT0_ORG?= 0x600 - -boot0: boot0.o - ${LD} -N -e start -Ttext ${BOOT_BOOT0_ORG} -o boot0.out boot0.o +# pull BOOT0_ORIGIN out of bootasm.h +# +# +boot0: boot0.o bootasmdef.nx + @echo "boot0 origin set to `${.OBJDIR}/bootasmdef.nx BOOT0_ORIGIN`" + ${LD} -N -e start -Ttext `${.OBJDIR}/bootasmdef.nx BOOT0_ORIGIN` -o boot0.out boot0.o objcopy -S -O binary boot0.out ${.TARGET} boot0.o: boot0.S ${CC} -c -DFLAGS=${BOOT_BOOT0_FLAGS} -DTICKS=${BOOT_BOOT0_TICKS} \ ${AFLAGS} ${.IMPSRC} -o ${.TARGET} -CLEANFILES+= boot0.out boot0.o +CLEANFILES+= boot0.out boot0.o bootasmdef.nx .include diff --git a/sys/boot/i386/boot0/boot0.S b/sys/boot/i386/boot0/boot0.S index 6d3cb7f0fc..42914e84b4 100644 --- a/sys/boot/i386/boot0/boot0.S +++ b/sys/boot/i386/boot0/boot0.S @@ -1,4 +1,36 @@ /* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * * Copyright (c) 1998 Robert Nordier * All rights reserved. * @@ -14,7 +46,7 @@ * * * $FreeBSD: src/sys/boot/i386/boot0/boot0.s,v 1.26 2003/06/01 20:41:04 obrien Exp $ - * $DragonFly: src/sys/boot/i386/boot0/Attic/boot0.S,v 1.5 2004/07/18 23:39:59 dillon Exp $ + * $DragonFly: src/sys/boot/i386/boot0/Attic/boot0.S,v 1.6 2004/07/19 23:30:31 dillon Exp $ */ #include "../bootasm.h" @@ -38,6 +70,11 @@ * Addresses in the sector of embedded data values. Accessed * with negative offsets from the end of the relocated sector * (%ebp). + * + * Note that %ebp is the base of our variable space and + * points at the end of the sector (base + 0x200). The + * fake partition and menu option is thus stored in the + * memory just after the boot0 sector. */ .set _NXTDRV,-0x48 # Next drive .set _OPT,-0x47 # Default option @@ -60,7 +97,7 @@ start: cld # String ops inc movw %ax,%es # Address movw %ax,%ds # data movw %ax,%ss # Set up - movw $LOAD,%sp # stack + movw $MEM_BIOS_LADDR,%sp # stack /* * Copy this code to the address it was linked for @@ -82,10 +119,13 @@ start: cld # String ops inc stosw # them /* - * Relocate to the new copy of the code. + * Relocate to the new copy of the code. Do not make + * assumptions with regard to a relative-PC near jump + * capability. */ incb -0xe(%di) # Sector number - jmp main-LOAD+BOOT0_ORIGIN # To relocated code + pushw $main # Jump to relocated code + retw /* * Check what flags were loaded with us, specifically, Use a @@ -166,7 +206,7 @@ main.5: incw %dx # Next item */ popw %ax # Drive number subb $0x80-0x1,%al # Does next - cmpb NHRDRV,%al # drive exist? (from BIOS?) + cmpb BDA_NHRDRV,%al # drive exist? (from BIOS?) jb main.6 # Yes /* @@ -276,7 +316,7 @@ main.12: cbtw # Option * for rewriting to the disk. */ movb %al,_OPT(%bp) # Save option - movw $FAKE,%si # Partition for write + lea _FAKE(%bp),%si # Partition for write movb (%si),%dl # Drive number movw %si,%bx # Partition for read cmpb $0x4,%al # F5 pressed? @@ -308,12 +348,12 @@ main.14: popw %si # Restore subb $'0',%dl # number /* - * load selected bootsector to the LOAD location in RAM. - * If it fails to read or isn't marked bootable, treat it - * as a bad selection. + * load selected bootsector to the MEM_BIOS_LADDR location + * in RAM. If it fails to read or isn't marked bootable, + * treat it as a bad selection. * XXX what does %si carry? */ -main.15: movw $LOAD,%bx # Address for read +main.15: movw $MEM_BIOS_LADDR,%bx # Address for read movb $0x2,%ah # Read sector callw intx13 # from disk jc main.10 # If error diff --git a/sys/boot/i386/boot2/Makefile b/sys/boot/i386/boot2/Makefile index 72360ada9c..cb27cc7b5f 100644 --- a/sys/boot/i386/boot2/Makefile +++ b/sys/boot/i386/boot2/Makefile @@ -1,5 +1,5 @@ # $FreeBSD: src/sys/boot/i386/boot2/Makefile,v 1.47 2003/06/26 03:51:57 peter Exp $ -# $DragonFly: src/sys/boot/i386/boot2/Attic/Makefile,v 1.8 2004/07/19 01:24:58 dillon Exp $ +# $DragonFly: src/sys/boot/i386/boot2/Attic/Makefile,v 1.9 2004/07/19 23:30:32 dillon Exp $ PROG= boot2 NOMAN= @@ -11,6 +11,10 @@ CLEANFILES= boot boot1 boot1.out boot1.o \ boot2.s sio.o NM?= nm +NXCFLAGS= +NXLDFLAGS= + +.PATH: ${.CURDIR}/.. # A value of 0x80 enables LBA support. B1FLAGS= 0x80 @@ -25,10 +29,12 @@ BTX= ${.OBJDIR}/../btx BTX= ${.CURDIR}/../btx .endif -# WARNING! These constants are also defined in bootasm.h +# These origins are taken from bootasm.h. # -REL1= 0x700 -ORG1= 0x7c00 +REL1 = `${.OBJDIR}/bootasmdef.nx BOOT1_ORIGIN` +REL1D = `${.OBJDIR}/bootasmdef.nx -d BOOT1_ORIGIN` +ORG1 = `${.OBJDIR}/bootasmdef.nx MEM_BIOS_LADDR` +ORG1D = `${.OBJDIR}/bootasmdef.nx -d MEM_BIOS_LADDR` # WARNING! The ORG2 calculation is the origin of boot2.bin relative to # the start of the BTX *USER* address space, not the start of physical @@ -66,7 +72,7 @@ boot: boot1 boot2 boot1: boot1.out objcopy -S -O binary boot1.out ${.TARGET} -boot1.out: boot1.o +boot1.out: boot1.o bootasmdef.nx ${LD} ${LDFLAGS} -e start -Ttext ${ORG1} -o ${.TARGET} boot1.o boot1.o: boot1.S @@ -79,12 +85,12 @@ boot2.o: boot2.c ${.CURDIR}/../../common/ufsread.c rm -f boot2.s.tmp ${AS} ${AFLAGS} -o boot2.o boot2.s -boot2.h: boot1.out +boot2.h: boot1.out bootasmdef.nx + @echo "NOTE: boot2 using xread from boot1 at ${REL1}" ${NM} -t d ${.ALLSRC} | awk '/([0-9])+ T xread/ \ { x = $$1 - ORG1; \ printf("#define XREADORG %#x\n", REL1 + x) }' \ - ORG1=`printf "%d" ${ORG1}` \ - REL1=`printf "%d" ${REL1}` > boot2.h + ORG1=${ORG1D} REL1=${REL1D} > boot2.h boot2: boot2.ldr boot2.bin ${BTX}/btx/btx btxld -v -E ${ORG2} -f bin -b ${BTX}/btx/btx -l boot2.ldr \ @@ -131,3 +137,5 @@ machine: CLEANFILES+= machine .endif +CLEANFILES+= bootasmdef.nx + diff --git a/sys/boot/i386/boot2/boot1.S b/sys/boot/i386/boot2/boot1.S index 6aa12cb858..735c74c306 100644 --- a/sys/boot/i386/boot2/boot1.S +++ b/sys/boot/i386/boot2/boot1.S @@ -1,4 +1,36 @@ /* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * * Copyright (c) 1998 Robert Nordier * All rights reserved. * @@ -13,7 +45,7 @@ * purpose. * * $FreeBSD: src/sys/boot/i386/boot2/boot1.s,v 1.23 2003/08/22 01:59:28 imp Exp $ - * $DragonFly: src/sys/boot/i386/boot2/Attic/boot1.S,v 1.6 2004/07/19 01:24:58 dillon Exp $ + * $DragonFly: src/sys/boot/i386/boot2/Attic/boot1.S,v 1.7 2004/07/19 23:30:32 dillon Exp $ */ #include "../bootasm.h" @@ -116,11 +148,11 @@ main: cld // String ops inc mov %cx,%ss // Set up mov $start,%sp // stack // -// Relocate ourself to MEM_REL. Since %cx == 0, the inc %ch sets -// %cx == 0x100. +// Relocate ourself to BOOT1_ORIGIN. Since %cx == 0, the inc %ch sets +// %cx == 0x100 (256 words == 512 bytes). // mov %sp,%si // Source - mov $MEM_REL,%di // Destination + mov $BOOT1_ORIGIN,%di // Destination incb %ch // Word count rep // Copy movsw // code @@ -178,23 +210,30 @@ main.4: xor %dx,%dx // Partition:drive // That entry point is relative to MEM_BTX_USR; thus boot2.bin starts // at 0xc000. // -main.5: mov %dx,MEM_ARG // Save args +// MEM_BTX_USR_ARG will be overwritten by the disk read and the relocation +// loop, so we must store the argument after completing said loops. +// +main.5: pushw %dx // Save args movb $NSECT,%dh // Sector count callw nread // Read disk mov $MEM_BTX_ORG,%bx // BTX mov 0xa(%bx),%si // Get BTX length and set add %bx,%si // %si to start of boot2.bin mov $MEM_BTX_USR+SIZ_PAG*2,%di // Client page 2 - mov $MEM_BTX_ORG+(NSECT-1)*SIZ_SEC,%cx // Byte + mov $MEM_BTX_ORG+(NSECT-1)*SIZ_SEC,%cx // Size of client data sub %si,%cx // count rep // Relocate movsb // client - sub %di,%cx // Byte count + popw MEM_BTX_USR_ARG // save (disk,slice) for boot2 + sub %di,%cx // count = 0x[1]0000 - DSTPTR xorb %al,%al // Zero assumed bss from rep // the end of boot2.bin stosb // up to 0x10000 callw seta20 // Enable A20 - jmp start+MEM_BTX_ENTRY-MEM_ORG // Start BTX + + // YYY + pushw $MEM_BTX_ENTRY // Start BTX + retw // // Enable A20 so we can access memory above 1 meg. // @@ -323,7 +362,7 @@ read.5: shlb %bl // buffer sub %al,0x2(%bp) // block count ja read // If not done read.6: retw // To caller -read.7: testb $FL_PACKET,%cs:MEM_REL+flags-start // LBA support enabled? +read.7: testb $FL_PACKET,%cs:BOOT1_ORIGIN+flags-start // LBA support enabled? jz ereturn // No, so return an error mov $0x55aa,%bx // Magic push %dx // Save diff --git a/sys/boot/i386/boot2/boot2.c b/sys/boot/i386/boot2/boot2.c index 04df85e761..c093b69766 100644 --- a/sys/boot/i386/boot2/boot2.c +++ b/sys/boot/i386/boot2/boot2.c @@ -1,4 +1,36 @@ -/*- +/* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * * Copyright (c) 1998 Robert Nordier * All rights reserved. * @@ -13,7 +45,7 @@ * purpose. * * $FreeBSD: src/sys/boot/i386/boot2/boot2.c,v 1.64 2003/08/25 23:28:31 obrien Exp $ - * $DragonFly: src/sys/boot/i386/boot2/Attic/boot2.c,v 1.11 2004/07/18 23:40:01 dillon Exp $ + * $DragonFly: src/sys/boot/i386/boot2/Attic/boot2.c,v 1.12 2004/07/19 23:30:32 dillon Exp $ */ #include #include @@ -221,10 +253,10 @@ main(void) dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base); v86.ctl = V86_FLAGS; - dsk.drive = *(uint8_t *)PTOV(MEM_ARG); + dsk.drive = *(uint8_t *)PTOV(MEM_BTX_USR_ARG); dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD; dsk.unit = dsk.drive & DRV_MASK; - dsk.slice = *(uint8_t *)PTOV(MEM_ARG + 1) + 1; + dsk.slice = *(uint8_t *)PTOV(MEM_BTX_USR_ARG + 1) + 1; bootinfo.bi_version = BOOTINFO_VERSION; bootinfo.bi_size = sizeof(bootinfo); bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */ diff --git a/sys/boot/i386/bootasm.h b/sys/boot/i386/bootasm.h index ad6c918a33..4eb956d155 100644 --- a/sys/boot/i386/bootasm.h +++ b/sys/boot/i386/bootasm.h @@ -31,33 +31,69 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/sys/boot/i386/Attic/bootasm.h,v 1.2 2004/07/19 01:24:57 dillon Exp $ + * $DragonFly: src/sys/boot/i386/Attic/bootasm.h,v 1.3 2004/07/19 23:30:29 dillon Exp $ */ -/* - * NOTE: MEM_REL and MEM_ORG also defined in boot2/Makefile +/* + * Set the bootloader address set. + * + * UNSET - default backwards compatible boot blocks + * 1 - experimental move addresses above 0x1000 and hardwire the user + * stack. + * 2 - experimental move addresses abobe 0x2000 and hardwire the user + * stack. */ -#define NHRDRV 0x475 -#define BOOT0_ORIGIN 0x600 /* boot0 relocated */ -#define FAKE 0x800 /* Fake partition entry */ -#define LOAD 0x7c00 /* Load address */ -#define BOOTINFO_SIZE 0x48 /* bootinfo structure size */ -#define MEM_ARG_SIZE 0x18 -#define MEM_PAGE_SIZE 0x1000 -#define USR_ARGSPACE 0x1000 /* BTX loader / ttl argspace reserved */ -#define USR_ARGOFFSET (BOOTINFO_SIZE+MEM_ARG_SIZE) +/* #define BOOT_NEWBOOTLOADER 2 */ -#define MEM_REL 0x700 /* Relocation address */ -#define MEM_ARG 0x900 /* Arguments */ -#define MEM_ORG 0x7c00 /* Origin */ +/* + * Various fixed constants that do not change + */ -#define BDA_BOOT 0x472 /* Boot howto flag */ #define BDA_MEM 0x413 /* Free memory */ #define BDA_KEYFLAGS 0x417 /* Keyboard shift-state flags */ #define BDA_SCR 0x449 /* Video mode */ #define BDA_POS 0x450 /* Cursor position */ +#define BDA_BOOT 0x472 /* Boot howto flag */ +#define BDA_NHRDRV 0x475 #define BDA_KEYBOARD 0x496 /* BDA byte with keyboard bit */ +/* + * Structural equivalences + */ +#define BOOTINFO_SIZE 0x48 /* bootinfo structure size */ +#define MEM_ARG_SIZE 0x18 +#define MEM_PAGE_SIZE 0x1000 +#define MEM_BTX_LDR_OFF MEM_PAGE_SIZE /* offset of btx in the loader */ +#define USR_ARGOFFSET (BOOTINFO_SIZE+MEM_ARG_SIZE) + +/* -------- WARNING, BOOT0 STACK BELOW MEM_BIOS_LADDR -------- */ +#define MEM_BIOS_LADDR 0x7c00 /* Load address (static/BIOS) */ + +/* + * NOTE: BOOT0_ORIGIN is extracted from this file and used in boot0/Makefile + * BOOT1_ORIGIN is extracted from this file and used in boot2/Makefile + * + * NOTE: boot0 has a variable space after its sector which contains + * the fake partition and other variables. ~128 bytes should be reserved + * for this variable space, but it may overlap BOOT1's data space. + */ + +#if !defined(BOOT_NEWBOOTLOADER) + +/************************************************************************ + * STANDARD BOOTLOADER ADDRESS SET * + ************************************************************************ + * + * + */ + +#define USR_ARGSPACE 0x1000 /* BTX loader / ttl argspace reserved */ + +#define BOOT0_ORIGIN 0x600 /* boot0 relocated to (512+128 bytes) */ +/* -------- WARNING, BOOT1 STACK BELOW BOOT1_ORIGIN ------- */ +#define BOOT1_ORIGIN 0x700 /* boot1 relocated to (512 bytes) */ +#define MEM_ARG 0x900 /* tmp arg store cdboot/pxeboot */ + #define MEM_BTX_ESP 0x1000 /* btxldr top of stack? */ #define MEM_BTX_START 0x1000 /* start of BTX memory */ #define MEM_BTX_ESP0 0x1800 /* Supervisor stack */ @@ -72,6 +108,9 @@ * NOTE: page table location is hardwired in /usr/src/usr.sbin/btxld/btx.h */ #define MEM_BTX_TBL 0x5000 /* Page tables */ +#define MEM_BTX_ZEND 0x7000 /* Zero from IDT to here in btx.S */ + +/******************** 0x7c00 BIOS LOAD ADDRESS (512 bytes) **********/ /* * NOTE: BOOT2_LOAD_BUF also determines where the btx loader and boot2.bin @@ -80,7 +119,93 @@ #define BOOT2_LOAD_BUF 0x8c00 /* boot1 loads boot2 */ #define MEM_BTX_ORG 0x9000 /* base of BTX code */ #define MEM_BTX_ENTRY 0x9010 /* BTX starts execution here */ +/* + * WARNING! The USR area may be messed around with in 16 bit code mode, + * data loaded should probably not cross 0xffff (e.g. boot2 loads + * ~8K at MEM_BTX_USR). + * + * MEM_BTX_USR is basically the segment offset BTX uses when + * running 'client' code. So address 0 in the client code will + * actually be physical address MEM_BTX_USR. + */ #define MEM_BTX_USR 0xa000 /* base of BTX client/user memory */ #define MEM_BTX_USR_ARG 0xa100 -#define MEM_BTX_LDR_OFF MEM_PAGE_SIZE /* offset of btx in the loader */ + +#elif defined(BOOT_NEWBOOTLOADER) && BOOT_NEWBOOTLOADER == 1 + +/************************************************************************ + * EXPERIMENTAL BOOTLOADER ADDRESS SET 1 * + ************************************************************************ + * + * + */ + +#define USR_ARGSPACE 0x1000 /* BTX loader / ttl argspace reserved */ +#define MEM_BTX_USR_STK 0x90000 /* (phys addr) btx client usr stack */ + +#define MEM_BTX_START 0x1000 /* (unchanged) */ +#define MEM_BTX_ESP0 0x1800 /* (unchanged) */ +#define MEM_BTX_BUF 0x1800 /* (unchanged) */ +#define MEM_BTX_ESP1 0x1e00 /* (unchanged) */ +#define MEM_BTX_IDT 0x1e00 /* (unchanged) */ +#define MEM_BTX_TSS 0x1f98 /* (unchanged) */ +#define MEM_BTX_MAP 0x2000 /* (unchanged) */ +#define MEM_BTX_DIR 0x4000 /* (unchanged) */ +#define MEM_BTX_TBL 0x5000 /* (unchanged) */ +#define MEM_BTX_ZEND 0x7000 /* (unchanged) */ + +#define MEM_BTX_ESP 0x7800 /* don't use 0x1000 */ +#define BOOT0_ORIGIN 0x7800 /* boot0 relocated */ +#define BOOT1_ORIGIN 0x7900 /* boot1 relocated (data only?) */ +#define MEM_ARG 0x7b00 /* cdboot/pxeboot disk/slice xfer */ +/******************** 0x7c00 BIOS LOAD ADDRESS (512 bytes) **********/ + +#define BOOT2_LOAD_BUF 0x8c00 /* (unchanged) */ +#define MEM_BTX_ORG 0x9000 /* (unchanged) */ +#define MEM_BTX_ENTRY 0x9010 /* (unchanged) */ + +#define MEM_BTX_USR 0xa000 /* (unchanged) */ +#define MEM_BTX_USR_ARG 0xa100 /* (unchanged) */ + + +#elif defined(BOOT_NEWBOOTLOADER) && BOOT_NEWBOOTLOADER == 2 + +/************************************************************************ + * EXPERIMENTAL BOOTLOADER ADDRESS SET 2 * + ************************************************************************ + * + * + */ +#define USR_ARGSPACE 0x1000 /* BTX loader / ttl argspace reserved */ +#define MEM_BTX_USR_STK 0x90000 /* (phys addr) btx client usr stack */ + +#define MEM_BTX_START 0x2000 /* (unchanged) */ +#define MEM_BTX_ESP0 0x2800 /* (unchanged) */ +#define MEM_BTX_BUF 0x2800 /* (unchanged) */ +#define MEM_BTX_ESP1 0x2e00 /* (unchanged) */ +#define MEM_BTX_IDT 0x2e00 /* (unchanged) */ +#define MEM_BTX_TSS 0x2f98 /* (unchanged) */ +#define MEM_BTX_MAP 0x3000 /* (unchanged) */ +#define MEM_BTX_DIR 0x5000 /* (unchanged) */ +/****** MEM_BTX_TBL (16K) SUPPORT REMOVED ***********************/ +#define MEM_BTX_ZEND 0x6000 /* (unchanged) */ + +#define MEM_BTX_ESP 0x7800 /* don't use 0x1000 */ +#define BOOT0_ORIGIN 0x7800 /* boot0 relocated */ +#define BOOT1_ORIGIN 0x7900 /* boot1 relocated (data only?) */ +#define MEM_ARG 0x7b00 /* cdboot/pxeboot disk/slice xfer */ +/******************** 0x7c00 BIOS LOAD ADDRESS (512 bytes) **********/ + +#define BOOT2_LOAD_BUF 0x8c00 /* (unchanged) */ +#define MEM_BTX_ORG 0x9000 /* (unchanged) */ +#define MEM_BTX_ENTRY 0x9010 /* (unchanged) */ + +#define MEM_BTX_USR 0xa000 /* base of BTX client/user memory */ +#define MEM_BTX_USR_ARG 0xa100 /* boot1->boot2 disk/slice xfer */ + +#else + +#error "BAD BOOT_NEWBOOTLOADER SETTING. UNSET TO GET DEFAULT" + +#endif /* BOOT_NEWBOOTLOADER */ diff --git a/sys/boot/i386/bootasmdef.c b/sys/boot/i386/bootasmdef.c new file mode 100644 index 0000000000..20a240fb27 --- /dev/null +++ b/sys/boot/i386/bootasmdef.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $DragonFly: src/sys/boot/i386/Attic/bootasmdef.c,v 1.1 2004/07/19 23:30:29 dillon Exp $ + */ + +#include +#include +#include +#include +#include "bootasm.h" + +static +void +usage(const char *arg0, int code) +{ + fprintf(stderr, "%s {BOOT0_ORIGIN,BOOT1_ORIGIN,MEM_BIOS_LADDR}\n", arg0); + exit(code); +} + +int +main(int ac, char **av) +{ + const char *fmt; + const char *var; + + if (ac == 1) + usage(av[0], 1); + if (strcmp(av[1], "-d") == 0) { + if (ac == 2) + usage(av[0], 1); + var = av[2]; + fmt = "%d\n"; + } else { + var = av[1]; + fmt = "0x%04x\n"; + } + + if (strcmp(var, "BOOT0_ORIGIN") == 0) { + printf(fmt, BOOT0_ORIGIN); + } else if (strcmp(var, "BOOT1_ORIGIN") == 0) { + printf(fmt, BOOT1_ORIGIN); + } else if (strcmp(var, "MEM_BIOS_LADDR") == 0) { + printf(fmt, MEM_BIOS_LADDR); + } else { + usage(av[0], 1); + } + return(0); +} + diff --git a/sys/boot/i386/btx/btx/btx.S b/sys/boot/i386/btx/btx/btx.S index 017623b4b0..1077cb7c7e 100644 --- a/sys/boot/i386/btx/btx/btx.S +++ b/sys/boot/i386/btx/btx/btx.S @@ -1,4 +1,36 @@ /* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * * Copyright (c) 1998 Robert Nordier * All rights reserved. * @@ -13,7 +45,7 @@ * purpose. * * $FreeBSD: src/sys/boot/i386/btx/btx/btx.s,v 1.32 2002/10/08 18:19:02 jhb Exp $ - * $DragonFly: src/sys/boot/i386/btx/btx/Attic/btx.S,v 1.6 2004/07/19 01:25:00 dillon Exp $ + * $DragonFly: src/sys/boot/i386/btx/btx/Attic/btx.S,v 1.7 2004/07/19 23:30:33 dillon Exp $ */ #include "../../bootasm.h" @@ -81,6 +113,8 @@ /* * Code segment. + * + * BTX start. */ .globl start .code16 @@ -115,7 +149,7 @@ init: cli # Disable interrupts * Initialize memory. */ mov $MEM_BTX_IDT,%di # Memory to initialize - mov $(MEM_BTX_ORG-MEM_BTX_IDT)/2,%cx # Words to zero + mov $(MEM_BTX_ZEND-MEM_BTX_IDT)/2,%cx # Words to zero push %di # Save rep # Zero-fill stosw # memory @@ -217,18 +251,31 @@ init.8: xorl %ecx,%ecx # Zero */ movb $SEL_TSS,%cl # Set task ltr %cx # register + + /* + * BTX user area base of VM, for converting physical stack + * addresses to btx-client virtual stack addresses. + */ + movl $MEM_BTX_USR,%edx +#if !defined(MEM_BTX_USR_STK) /* * XXX We should NOT use BDA_MEM here. Use a fixed location - * instead. + * instead. (%eax is a physical stack addr) */ - movl $MEM_BTX_USR,%edx # User base address movzwl %ss:BDA_MEM,%eax # Get free memory shll $0xa,%eax # To bytes +#else + /* + * Use a fixed user stack instead of depending on BDA_MEM. + * %eax is a physical * stack address. + */ + movl $MEM_BTX_USR_STK,%eax +#endif subl $USR_ARGSPACE,%eax # Less arg space - subl %edx,%eax # Less base + subl %edx,%eax # Less base Phys->Virt movb $SEL_UDATA,%cl # User data selector pushl %ecx # Set SS - pushl %eax # Set ESP + pushl %eax # Set ESP (virtual address) push $0x202 # Set flags (IF set) push $SEL_UCODE # Set CS pushl btx_hdr+0xc # Set EIP @@ -236,7 +283,7 @@ init.8: xorl %ecx,%ecx # Zero pushl %ecx # Set FS pushl %ecx # Set DS pushl %ecx # Set ES - pushl %edx # Set EAX + pushl %edx # Set EAX (phys base addr of VM) movb $0x7,%cl # Set remaining init.9: push $0x0 # general loop init.9 # registers @@ -1127,6 +1174,21 @@ putchr.4: movw %dx,(%ebx) # Update position /* * Global descriptor table. + * + * 16: segment extent lsb + * 24: segment base lsb + * + * 5:TYPE + * 2:DPL + * 1:PRESENT + * + * 4: segment extent msb + * 2: unused + * 1: 32 bit, else 16 bit + * 1: limit granularity byte/page units + + * 8: segment base msb + * */ .p2align 4 gdt: .word 0x0,0x0,0x0,0x0 # Null entry diff --git a/sys/boot/i386/btx/btxldr/Makefile b/sys/boot/i386/btx/btxldr/Makefile index 3d92d6aab7..fe3baba59e 100644 --- a/sys/boot/i386/btx/btxldr/Makefile +++ b/sys/boot/i386/btx/btxldr/Makefile @@ -1,8 +1,9 @@ # $FreeBSD: src/sys/boot/i386/btx/btxldr/Makefile,v 1.12 2003/06/26 03:51:57 peter Exp $ -# $DragonFly: src/sys/boot/i386/btx/btxldr/Attic/Makefile,v 1.4 2004/06/25 00:38:38 dillon Exp $ +# $DragonFly: src/sys/boot/i386/btx/btxldr/Attic/Makefile,v 1.5 2004/07/19 23:30:34 dillon Exp $ CFLAGS+= -DLOADER_ADDRESS=${LOADER_ADDRESS} +# BTXLDR_VERBOSE=YES .if defined(BTXLDR_VERBOSE) CFLAGS+= -DBTXLDR_VERBOSE .endif diff --git a/sys/boot/i386/btx/btxldr/btxldr.S b/sys/boot/i386/btx/btxldr/btxldr.S index f6d1cca086..f9f10cf86c 100644 --- a/sys/boot/i386/btx/btxldr/btxldr.S +++ b/sys/boot/i386/btx/btxldr/btxldr.S @@ -1,4 +1,36 @@ /* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * * Copyright (c) 1998 Robert Nordier * All rights reserved. * @@ -13,7 +45,7 @@ * purpose. * * $FreeBSD: src/sys/boot/i386/btx/btxldr/Makefile,v 1.17 2004/04/27 19:45:16 ru Exp $ - * $DragonFly: src/sys/boot/i386/btx/btxldr/Attic/btxldr.S,v 1.2 2004/07/19 01:25:01 dillon Exp $ + * $DragonFly: src/sys/boot/i386/btx/btxldr/Attic/btxldr.S,v 1.3 2004/07/19 23:30:34 dillon Exp $ */ /* @@ -50,6 +82,11 @@ .set SIZ_STUB,0x1a # Size of stub /* * We expect to be loaded by boot2 at the origin defined in ./Makefile. + * This is typically 0x200000. + * + * I *THINK* (not sure) that execution begins with us in 'virtual mode', + * meaning everything is offset by MEM_BTX_USR. We will load a gdt to + * set the base offsets back to 0. */ .globl start /* @@ -58,18 +95,29 @@ start: cld # String ops inc movl $m_logo,%esi # Identify call putstr # ourselves +#if !defined(MEM_BTX_USR_STK) movzwl BDA_MEM,%eax # Get base memory shll $0xa,%eax # Convert to bytes +#else + movl $MEM_BTX_USR_STK,%eax +#endif movl %eax,%ebp # Base of user stack #ifdef BTXLDR_VERBOSE movl $m_mem,%esi # Display call hexout # amount of call putstr # base memory #endif - lgdt gdtdesc # Load new GDT -/* - * Relocate caller's arguments. - */ + + /* + * Load a new GDT. XXX what does this do to running code + * segments? What if an interrupt occurs? What if the + * segment registers are reloaded? + */ + lgdt gdtdesc + + /* + * Relocate caller's arguments. + */ #ifdef BTXLDR_VERBOSE movl $m_esp,%esi # Display movl %esp,%eax # caller @@ -142,19 +190,33 @@ start_null_bi: movl $0x18,%ecx # Allocate space addb $'0',%al # Display call putchr # units call putstr # End message - movl %ebx,%esi # BTX image - movzwl 0x8(%ebx),%edi # Compute - orl $PAG_SIZ/PAG_ENT-1,%edi # the - incl %edi # BTX - shll $0x2,%edi # load - addl $MEM_BTX_TBL,%edi # address + + # Relocate the BTX image from wherever it was loaded (%ebx), + # which is typically offset 0x1000 in the load data, to + # MEM_BTX_ORG (typically 0x9000). + # + # MEM_BTX_TBL + ((mappages | 0x3ff) + 1) * 4 + # mappages is typically 0x0ffn so we get 0x1000*4 = 0x4000 + # MEM_BTX_TBL is traditionally mapped at 0x5000 so the + # whole calculation translated to MEM_BTX_ORG (0x9000). +#if 0 + /* XXX what is all of this junk? */ + movzwl 0x8(%ebx),%edi # Compute the BTX load address + orl $PAG_SIZ/PAG_ENT-1,%edi # (by skipping the page table) + incl %edi + shll $0x2,%edi + addl $MEM_BTX_TBL,%edi +#else + movl $MEM_BTX_ORG,%edi +#endif + movl %ebx,%esi # %esi = BTX image source pushl %edi # Save load address - movzwl 0xa(%ebx),%ecx # Image size + movzwl 0xa(%ebx),%ecx # Image size (bytes) #ifdef BTXLDR_VERBOSE pushl %ecx # Save image size #endif - rep # Relocate - movsb # BTX + rep # Relocate BTX + movsb movl %esi,%ebx # Keep place #ifdef BTXLDR_VERBOSE movl $m_rel_btx,%esi # Restore @@ -167,7 +229,12 @@ start_null_bi: movl $0x18,%ecx # Allocate space call hexout # relocation call putstr # message #endif - addl $PAG_SIZ,%ebp # Display + /* + * ADJUST EBP FOR USER BASE ADDRESS + * + * XXX why not just move MEM_BTX_USR into %ebp ? + */ + addl $MEM_BTX_USR-MEM_BTX_ORG,%ebp #ifdef BTXLDR_VERBOSE movl $m_base,%esi # the movl %ebp,%eax # user diff --git a/sys/boot/i386/btx/lib/btxcsu.S b/sys/boot/i386/btx/lib/btxcsu.S index cf58b7ccd8..49c2e94a87 100644 --- a/sys/boot/i386/btx/lib/btxcsu.S +++ b/sys/boot/i386/btx/lib/btxcsu.S @@ -13,11 +13,11 @@ * purpose. * * $FreeBSD: src/sys/boot/i386/btx/lib/btxcsu.s,v 1.3 1999/08/28 00:40:07 peter Exp $ - * $DragonFly: src/sys/boot/i386/btx/lib/Attic/btxcsu.S,v 1.4 2004/07/19 01:25:02 dillon Exp $ + * $DragonFly: src/sys/boot/i386/btx/lib/Attic/btxcsu.S,v 1.5 2004/07/19 23:30:35 dillon Exp $ */ /* - * BTX C startup code (ELF). + * BTX C startup code (ELF). Executed from ../btx/btx.S */ #include "../../bootasm.h" @@ -31,6 +31,7 @@ * Client entry point. */ _start: movl %eax,__base # Set base address + # for Virt->Phys conversions movl %esp,%eax # Get base of arguments addl $USR_ARGSPACE-USR_ARGOFFSET,%eax movl %eax,__args diff --git a/sys/boot/i386/cdboot/cdboot.S b/sys/boot/i386/cdboot/cdboot.S index 8de9f2d0c3..58ebe673a9 100644 --- a/sys/boot/i386/cdboot/cdboot.S +++ b/sys/boot/i386/cdboot/cdboot.S @@ -14,7 +14,7 @@ * * * $FreeBSD: src/sys/boot/i386/cdboot/cdboot.s,v 1.9 2001/11/07 01:20:33 jhb Exp $ - * $DragonFly: src/sys/boot/i386/cdboot/Attic/cdboot.S,v 1.5 2004/07/18 23:40:07 dillon Exp $ + * $DragonFly: src/sys/boot/i386/cdboot/Attic/cdboot.S,v 1.6 2004/07/19 23:30:36 dillon Exp $ */ /* @@ -248,7 +248,7 @@ pm_end: sti # Turn interrupts back on now * can see them. */ mov $MEM_ARG,%si # where the args are at now - mov $MEM_BTX_USR_ARG,%di # where the args are moving to + mov $MEM_BTX_USR_ARG,%di # where the args are moving to mov $(MEM_ARG_SIZE/4),%cx # size of the arguments in longs rep # Relocate movsl # the words diff --git a/sys/boot/i386/libi386/amd64_tramp.S b/sys/boot/i386/libi386/amd64_tramp.S index d72ffca68c..d590ba8123 100644 --- a/sys/boot/i386/libi386/amd64_tramp.S +++ b/sys/boot/i386/libi386/amd64_tramp.S @@ -24,9 +24,11 @@ * SUCH DAMAGE. * * $FreeBSD: src/sys/boot/i386/libi386/amd64_tramp.S,v 1.2 2003/05/17 00:30:51 peter Exp $ - * $DragonFly: src/sys/boot/i386/libi386/Attic/amd64_tramp.S,v 1.2 2003/11/22 19:30:55 asmodai Exp $ + * $DragonFly: src/sys/boot/i386/libi386/Attic/amd64_tramp.S,v 1.3 2004/07/19 23:30:37 dillon Exp $ */ +#include "../bootasm.h" + /* * Quick and dirty trampoline to get into 64 bit (long) mode and running * with paging enabled so that we enter the kernel at its linked address. @@ -38,8 +40,7 @@ #define CR0_PG 0x80000000 /* GRRR. Deal with BTX that links us for a non-zero location */ -#define VPBASE 0xa000 -#define VTOP(x) ((x) + VPBASE) +#define VTOP(x) ((x) + MEM_BTX_USR) .data diff --git a/sys/boot/i386/loader/Makefile b/sys/boot/i386/loader/Makefile index 9f8f8a063f..998a13e62b 100644 --- a/sys/boot/i386/loader/Makefile +++ b/sys/boot/i386/loader/Makefile @@ -1,5 +1,6 @@ # $FreeBSD: src/sys/boot/i386/loader/Makefile,v 1.66 2003/06/26 03:51:57 peter Exp $ -# $DragonFly: src/sys/boot/i386/loader/Attic/Makefile,v 1.5 2004/01/28 19:24:14 dillon Exp $ +# $DragonFly: src/sys/boot/i386/loader/Attic/Makefile,v 1.6 2004/07/19 23:30:38 dillon Exp $ +# PROG= loader STRIP= @@ -7,6 +8,14 @@ NEWVERSWHAT= "bootstrap loader" i386 BINDIR?= /boot INSTALLFLAGS= -b +# Notes: the 'loader' client is brought in with a virtual base of (typically) +# 0xa000 and a code offset of 0x0. The btxldr is loaded at 0x200000 (?). +# +# MEM_BTX_USR_ARG is not used in the boot2->loader stage, only in the +# boot1->boot2 stage (XXX what about the use in cdboot and pxeboot?) +# +ORG= 0x0 + # architecture-specific loader code SRCS= main.c conf.c @@ -49,7 +58,7 @@ CFLAGS+= -I${.CURDIR}/../../.. -I. CLEANFILES+= vers.c vers.o ${PROG}.list ${PROG}.bin ${PROG}.sym ${PROG}.help CFLAGS+= -Wall -LDFLAGS= -nostdlib -static -Ttext 0x0 +LDFLAGS= -nostdlib -static -Ttext ${ORG} # i386 standalone support library LIBI386= ${.OBJDIR}/../libi386/libi386.a diff --git a/sys/boot/i386/loader/main.c b/sys/boot/i386/loader/main.c index 812d505bd5..e58a03e3ea 100644 --- a/sys/boot/i386/loader/main.c +++ b/sys/boot/i386/loader/main.c @@ -1,4 +1,36 @@ -/*- +/* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * * Copyright (c) 1998 Michael Smith * All rights reserved. * @@ -24,7 +56,7 @@ * SUCH DAMAGE. * * $FreeBSD: src/sys/boot/i386/loader/main.c,v 1.28 2003/08/25 23:28:32 obrien Exp $ - * $DragonFly: src/sys/boot/i386/loader/Attic/main.c,v 1.6 2004/06/26 22:37:11 dillon Exp $ + * $DragonFly: src/sys/boot/i386/loader/Attic/main.c,v 1.7 2004/07/19 23:30:38 dillon Exp $ */ /* @@ -72,6 +104,7 @@ extern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[]; /* XXX debugging */ extern char end[]; +#define COMCONSOLE_DEBUG #ifdef COMCONSOLE_DEBUG static void @@ -98,10 +131,12 @@ WDEBUG(char c) #endif +static void nop(void) { } int main(void) { - int i; + char *memend; + int i; WDEBUG_INIT(); WDEBUG('X'); @@ -119,15 +154,28 @@ main(void) /* * Initialize the heap as early as possible. Once this is done, - * malloc() is usable. + * malloc() is usable. + * + * Don't include our stack in the heap. If the stack is in low + * user memory use {end,bios_basemem}. If the stack is in high + * user memory but not extended memory then don't let the heap + * overlap the stack. If the stack is in extended memory limit + * the heap to bios_basemem. + * + * Be sure to use the virtual bios_basemem address rather then + * the physical bios_basemem address or we may overwrite BIOS + * data. */ bios_getmem(); -#if 0 /* FUTURE */ - if (bios_basemem > 0x9f000) - bios_basemem = 0x9f000; -#endif - - setheap((void *)end, (void *)bios_basemem); + memend = (char *)&memend - 0x8000; /* space for stack */ + memend = (char *)((uintptr_t)memend & ~(uintptr_t)(0x1000 - 1)); + if (memend < (char *)end) { + setheap((void *)end, PTOV(bios_basemem)); + } else { + if (memend > (char *)PTOV(bios_basemem)) + memend = (char *)PTOV(bios_basemem); + setheap((void *)end, memend); + } /* * XXX Chicken-and-egg problem; we want to have console output early, diff --git a/sys/boot/pc32/boot0/Makefile b/sys/boot/pc32/boot0/Makefile index c05b04bc78..85419f375b 100644 --- a/sys/boot/pc32/boot0/Makefile +++ b/sys/boot/pc32/boot0/Makefile @@ -1,5 +1,5 @@ # $FreeBSD: src/sys/boot/i386/boot0/Makefile,v 1.17 2002/09/17 01:48:54 peter Exp $ -# $DragonFly: src/sys/boot/pc32/boot0/Makefile,v 1.4 2004/07/18 23:39:59 dillon Exp $ +# $DragonFly: src/sys/boot/pc32/boot0/Makefile,v 1.5 2004/07/19 23:30:31 dillon Exp $ PROG= boot0 NOMAN= @@ -7,6 +7,8 @@ STRIP= BINDIR?= /boot BINMODE= 444 +.PATH: ${.CURDIR}/.. + # The default set of flags compiled into boot0. This enables update (writing # the modified boot0 back to disk after running so that the selection made is # saved), packet mode (detect and use the BIOS EDD extensions if we try to @@ -18,18 +20,18 @@ BOOT_BOOT0_FLAGS?= 0xf # 0xb6 (182d) corresponds to 10 seconds. BOOT_BOOT0_TICKS?= 0xb6 -# The base address that we the boot0 code to to run it. Don't change this -# unless you are glutton for punishment. -BOOT_BOOT0_ORG?= 0x600 - -boot0: boot0.o - ${LD} -N -e start -Ttext ${BOOT_BOOT0_ORG} -o boot0.out boot0.o +# pull BOOT0_ORIGIN out of bootasm.h +# +# +boot0: boot0.o bootasmdef.nx + @echo "boot0 origin set to `${.OBJDIR}/bootasmdef.nx BOOT0_ORIGIN`" + ${LD} -N -e start -Ttext `${.OBJDIR}/bootasmdef.nx BOOT0_ORIGIN` -o boot0.out boot0.o objcopy -S -O binary boot0.out ${.TARGET} boot0.o: boot0.S ${CC} -c -DFLAGS=${BOOT_BOOT0_FLAGS} -DTICKS=${BOOT_BOOT0_TICKS} \ ${AFLAGS} ${.IMPSRC} -o ${.TARGET} -CLEANFILES+= boot0.out boot0.o +CLEANFILES+= boot0.out boot0.o bootasmdef.nx .include diff --git a/sys/boot/pc32/boot0/boot0.S b/sys/boot/pc32/boot0/boot0.S index 58a2492f83..a7ab2c5ed4 100644 --- a/sys/boot/pc32/boot0/boot0.S +++ b/sys/boot/pc32/boot0/boot0.S @@ -1,4 +1,36 @@ /* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * * Copyright (c) 1998 Robert Nordier * All rights reserved. * @@ -14,7 +46,7 @@ * * * $FreeBSD: src/sys/boot/i386/boot0/boot0.s,v 1.26 2003/06/01 20:41:04 obrien Exp $ - * $DragonFly: src/sys/boot/pc32/boot0/boot0.S,v 1.5 2004/07/18 23:39:59 dillon Exp $ + * $DragonFly: src/sys/boot/pc32/boot0/boot0.S,v 1.6 2004/07/19 23:30:31 dillon Exp $ */ #include "../bootasm.h" @@ -38,6 +70,11 @@ * Addresses in the sector of embedded data values. Accessed * with negative offsets from the end of the relocated sector * (%ebp). + * + * Note that %ebp is the base of our variable space and + * points at the end of the sector (base + 0x200). The + * fake partition and menu option is thus stored in the + * memory just after the boot0 sector. */ .set _NXTDRV,-0x48 # Next drive .set _OPT,-0x47 # Default option @@ -60,7 +97,7 @@ start: cld # String ops inc movw %ax,%es # Address movw %ax,%ds # data movw %ax,%ss # Set up - movw $LOAD,%sp # stack + movw $MEM_BIOS_LADDR,%sp # stack /* * Copy this code to the address it was linked for @@ -82,10 +119,13 @@ start: cld # String ops inc stosw # them /* - * Relocate to the new copy of the code. + * Relocate to the new copy of the code. Do not make + * assumptions with regard to a relative-PC near jump + * capability. */ incb -0xe(%di) # Sector number - jmp main-LOAD+BOOT0_ORIGIN # To relocated code + pushw $main # Jump to relocated code + retw /* * Check what flags were loaded with us, specifically, Use a @@ -166,7 +206,7 @@ main.5: incw %dx # Next item */ popw %ax # Drive number subb $0x80-0x1,%al # Does next - cmpb NHRDRV,%al # drive exist? (from BIOS?) + cmpb BDA_NHRDRV,%al # drive exist? (from BIOS?) jb main.6 # Yes /* @@ -276,7 +316,7 @@ main.12: cbtw # Option * for rewriting to the disk. */ movb %al,_OPT(%bp) # Save option - movw $FAKE,%si # Partition for write + lea _FAKE(%bp),%si # Partition for write movb (%si),%dl # Drive number movw %si,%bx # Partition for read cmpb $0x4,%al # F5 pressed? @@ -308,12 +348,12 @@ main.14: popw %si # Restore subb $'0',%dl # number /* - * load selected bootsector to the LOAD location in RAM. - * If it fails to read or isn't marked bootable, treat it - * as a bad selection. + * load selected bootsector to the MEM_BIOS_LADDR location + * in RAM. If it fails to read or isn't marked bootable, + * treat it as a bad selection. * XXX what does %si carry? */ -main.15: movw $LOAD,%bx # Address for read +main.15: movw $MEM_BIOS_LADDR,%bx # Address for read movb $0x2,%ah # Read sector callw intx13 # from disk jc main.10 # If error diff --git a/sys/boot/pc32/boot2/Makefile b/sys/boot/pc32/boot2/Makefile index 7915a8b242..35bff5400b 100644 --- a/sys/boot/pc32/boot2/Makefile +++ b/sys/boot/pc32/boot2/Makefile @@ -1,5 +1,5 @@ # $FreeBSD: src/sys/boot/i386/boot2/Makefile,v 1.47 2003/06/26 03:51:57 peter Exp $ -# $DragonFly: src/sys/boot/pc32/boot2/Makefile,v 1.8 2004/07/19 01:24:58 dillon Exp $ +# $DragonFly: src/sys/boot/pc32/boot2/Makefile,v 1.9 2004/07/19 23:30:32 dillon Exp $ PROG= boot2 NOMAN= @@ -11,6 +11,10 @@ CLEANFILES= boot boot1 boot1.out boot1.o \ boot2.s sio.o NM?= nm +NXCFLAGS= +NXLDFLAGS= + +.PATH: ${.CURDIR}/.. # A value of 0x80 enables LBA support. B1FLAGS= 0x80 @@ -25,10 +29,12 @@ BTX= ${.OBJDIR}/../btx BTX= ${.CURDIR}/../btx .endif -# WARNING! These constants are also defined in bootasm.h +# These origins are taken from bootasm.h. # -REL1= 0x700 -ORG1= 0x7c00 +REL1 = `${.OBJDIR}/bootasmdef.nx BOOT1_ORIGIN` +REL1D = `${.OBJDIR}/bootasmdef.nx -d BOOT1_ORIGIN` +ORG1 = `${.OBJDIR}/bootasmdef.nx MEM_BIOS_LADDR` +ORG1D = `${.OBJDIR}/bootasmdef.nx -d MEM_BIOS_LADDR` # WARNING! The ORG2 calculation is the origin of boot2.bin relative to # the start of the BTX *USER* address space, not the start of physical @@ -66,7 +72,7 @@ boot: boot1 boot2 boot1: boot1.out objcopy -S -O binary boot1.out ${.TARGET} -boot1.out: boot1.o +boot1.out: boot1.o bootasmdef.nx ${LD} ${LDFLAGS} -e start -Ttext ${ORG1} -o ${.TARGET} boot1.o boot1.o: boot1.S @@ -79,12 +85,12 @@ boot2.o: boot2.c ${.CURDIR}/../../common/ufsread.c rm -f boot2.s.tmp ${AS} ${AFLAGS} -o boot2.o boot2.s -boot2.h: boot1.out +boot2.h: boot1.out bootasmdef.nx + @echo "NOTE: boot2 using xread from boot1 at ${REL1}" ${NM} -t d ${.ALLSRC} | awk '/([0-9])+ T xread/ \ { x = $$1 - ORG1; \ printf("#define XREADORG %#x\n", REL1 + x) }' \ - ORG1=`printf "%d" ${ORG1}` \ - REL1=`printf "%d" ${REL1}` > boot2.h + ORG1=${ORG1D} REL1=${REL1D} > boot2.h boot2: boot2.ldr boot2.bin ${BTX}/btx/btx btxld -v -E ${ORG2} -f bin -b ${BTX}/btx/btx -l boot2.ldr \ @@ -131,3 +137,5 @@ machine: CLEANFILES+= machine .endif +CLEANFILES+= bootasmdef.nx + diff --git a/sys/boot/pc32/boot2/boot1.S b/sys/boot/pc32/boot2/boot1.S index 4413a66046..1808756ebe 100644 --- a/sys/boot/pc32/boot2/boot1.S +++ b/sys/boot/pc32/boot2/boot1.S @@ -1,4 +1,36 @@ /* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * * Copyright (c) 1998 Robert Nordier * All rights reserved. * @@ -13,7 +45,7 @@ * purpose. * * $FreeBSD: src/sys/boot/i386/boot2/boot1.s,v 1.23 2003/08/22 01:59:28 imp Exp $ - * $DragonFly: src/sys/boot/pc32/boot2/boot1.S,v 1.6 2004/07/19 01:24:58 dillon Exp $ + * $DragonFly: src/sys/boot/pc32/boot2/boot1.S,v 1.7 2004/07/19 23:30:32 dillon Exp $ */ #include "../bootasm.h" @@ -116,11 +148,11 @@ main: cld // String ops inc mov %cx,%ss // Set up mov $start,%sp // stack // -// Relocate ourself to MEM_REL. Since %cx == 0, the inc %ch sets -// %cx == 0x100. +// Relocate ourself to BOOT1_ORIGIN. Since %cx == 0, the inc %ch sets +// %cx == 0x100 (256 words == 512 bytes). // mov %sp,%si // Source - mov $MEM_REL,%di // Destination + mov $BOOT1_ORIGIN,%di // Destination incb %ch // Word count rep // Copy movsw // code @@ -178,23 +210,30 @@ main.4: xor %dx,%dx // Partition:drive // That entry point is relative to MEM_BTX_USR; thus boot2.bin starts // at 0xc000. // -main.5: mov %dx,MEM_ARG // Save args +// MEM_BTX_USR_ARG will be overwritten by the disk read and the relocation +// loop, so we must store the argument after completing said loops. +// +main.5: pushw %dx // Save args movb $NSECT,%dh // Sector count callw nread // Read disk mov $MEM_BTX_ORG,%bx // BTX mov 0xa(%bx),%si // Get BTX length and set add %bx,%si // %si to start of boot2.bin mov $MEM_BTX_USR+SIZ_PAG*2,%di // Client page 2 - mov $MEM_BTX_ORG+(NSECT-1)*SIZ_SEC,%cx // Byte + mov $MEM_BTX_ORG+(NSECT-1)*SIZ_SEC,%cx // Size of client data sub %si,%cx // count rep // Relocate movsb // client - sub %di,%cx // Byte count + popw MEM_BTX_USR_ARG // save (disk,slice) for boot2 + sub %di,%cx // count = 0x[1]0000 - DSTPTR xorb %al,%al // Zero assumed bss from rep // the end of boot2.bin stosb // up to 0x10000 callw seta20 // Enable A20 - jmp start+MEM_BTX_ENTRY-MEM_ORG // Start BTX + + // YYY + pushw $MEM_BTX_ENTRY // Start BTX + retw // // Enable A20 so we can access memory above 1 meg. // @@ -323,7 +362,7 @@ read.5: shlb %bl // buffer sub %al,0x2(%bp) // block count ja read // If not done read.6: retw // To caller -read.7: testb $FL_PACKET,%cs:MEM_REL+flags-start // LBA support enabled? +read.7: testb $FL_PACKET,%cs:BOOT1_ORIGIN+flags-start // LBA support enabled? jz ereturn // No, so return an error mov $0x55aa,%bx // Magic push %dx // Save diff --git a/sys/boot/pc32/boot2/boot2.c b/sys/boot/pc32/boot2/boot2.c index 364a4aa27a..189a53aab5 100644 --- a/sys/boot/pc32/boot2/boot2.c +++ b/sys/boot/pc32/boot2/boot2.c @@ -1,4 +1,36 @@ -/*- +/* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * * Copyright (c) 1998 Robert Nordier * All rights reserved. * @@ -13,7 +45,7 @@ * purpose. * * $FreeBSD: src/sys/boot/i386/boot2/boot2.c,v 1.64 2003/08/25 23:28:31 obrien Exp $ - * $DragonFly: src/sys/boot/pc32/boot2/boot2.c,v 1.11 2004/07/18 23:40:01 dillon Exp $ + * $DragonFly: src/sys/boot/pc32/boot2/boot2.c,v 1.12 2004/07/19 23:30:32 dillon Exp $ */ #include #include @@ -221,10 +253,10 @@ main(void) dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base); v86.ctl = V86_FLAGS; - dsk.drive = *(uint8_t *)PTOV(MEM_ARG); + dsk.drive = *(uint8_t *)PTOV(MEM_BTX_USR_ARG); dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD; dsk.unit = dsk.drive & DRV_MASK; - dsk.slice = *(uint8_t *)PTOV(MEM_ARG + 1) + 1; + dsk.slice = *(uint8_t *)PTOV(MEM_BTX_USR_ARG + 1) + 1; bootinfo.bi_version = BOOTINFO_VERSION; bootinfo.bi_size = sizeof(bootinfo); bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */ diff --git a/sys/boot/pc32/bootasm.h b/sys/boot/pc32/bootasm.h index be5bf43cee..bfb2f9a65b 100644 --- a/sys/boot/pc32/bootasm.h +++ b/sys/boot/pc32/bootasm.h @@ -31,33 +31,69 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/sys/boot/pc32/bootasm.h,v 1.2 2004/07/19 01:24:57 dillon Exp $ + * $DragonFly: src/sys/boot/pc32/bootasm.h,v 1.3 2004/07/19 23:30:29 dillon Exp $ */ -/* - * NOTE: MEM_REL and MEM_ORG also defined in boot2/Makefile +/* + * Set the bootloader address set. + * + * UNSET - default backwards compatible boot blocks + * 1 - experimental move addresses above 0x1000 and hardwire the user + * stack. + * 2 - experimental move addresses abobe 0x2000 and hardwire the user + * stack. */ -#define NHRDRV 0x475 -#define BOOT0_ORIGIN 0x600 /* boot0 relocated */ -#define FAKE 0x800 /* Fake partition entry */ -#define LOAD 0x7c00 /* Load address */ -#define BOOTINFO_SIZE 0x48 /* bootinfo structure size */ -#define MEM_ARG_SIZE 0x18 -#define MEM_PAGE_SIZE 0x1000 -#define USR_ARGSPACE 0x1000 /* BTX loader / ttl argspace reserved */ -#define USR_ARGOFFSET (BOOTINFO_SIZE+MEM_ARG_SIZE) +/* #define BOOT_NEWBOOTLOADER 2 */ -#define MEM_REL 0x700 /* Relocation address */ -#define MEM_ARG 0x900 /* Arguments */ -#define MEM_ORG 0x7c00 /* Origin */ +/* + * Various fixed constants that do not change + */ -#define BDA_BOOT 0x472 /* Boot howto flag */ #define BDA_MEM 0x413 /* Free memory */ #define BDA_KEYFLAGS 0x417 /* Keyboard shift-state flags */ #define BDA_SCR 0x449 /* Video mode */ #define BDA_POS 0x450 /* Cursor position */ +#define BDA_BOOT 0x472 /* Boot howto flag */ +#define BDA_NHRDRV 0x475 #define BDA_KEYBOARD 0x496 /* BDA byte with keyboard bit */ +/* + * Structural equivalences + */ +#define BOOTINFO_SIZE 0x48 /* bootinfo structure size */ +#define MEM_ARG_SIZE 0x18 +#define MEM_PAGE_SIZE 0x1000 +#define MEM_BTX_LDR_OFF MEM_PAGE_SIZE /* offset of btx in the loader */ +#define USR_ARGOFFSET (BOOTINFO_SIZE+MEM_ARG_SIZE) + +/* -------- WARNING, BOOT0 STACK BELOW MEM_BIOS_LADDR -------- */ +#define MEM_BIOS_LADDR 0x7c00 /* Load address (static/BIOS) */ + +/* + * NOTE: BOOT0_ORIGIN is extracted from this file and used in boot0/Makefile + * BOOT1_ORIGIN is extracted from this file and used in boot2/Makefile + * + * NOTE: boot0 has a variable space after its sector which contains + * the fake partition and other variables. ~128 bytes should be reserved + * for this variable space, but it may overlap BOOT1's data space. + */ + +#if !defined(BOOT_NEWBOOTLOADER) + +/************************************************************************ + * STANDARD BOOTLOADER ADDRESS SET * + ************************************************************************ + * + * + */ + +#define USR_ARGSPACE 0x1000 /* BTX loader / ttl argspace reserved */ + +#define BOOT0_ORIGIN 0x600 /* boot0 relocated to (512+128 bytes) */ +/* -------- WARNING, BOOT1 STACK BELOW BOOT1_ORIGIN ------- */ +#define BOOT1_ORIGIN 0x700 /* boot1 relocated to (512 bytes) */ +#define MEM_ARG 0x900 /* tmp arg store cdboot/pxeboot */ + #define MEM_BTX_ESP 0x1000 /* btxldr top of stack? */ #define MEM_BTX_START 0x1000 /* start of BTX memory */ #define MEM_BTX_ESP0 0x1800 /* Supervisor stack */ @@ -72,6 +108,9 @@ * NOTE: page table location is hardwired in /usr/src/usr.sbin/btxld/btx.h */ #define MEM_BTX_TBL 0x5000 /* Page tables */ +#define MEM_BTX_ZEND 0x7000 /* Zero from IDT to here in btx.S */ + +/******************** 0x7c00 BIOS LOAD ADDRESS (512 bytes) **********/ /* * NOTE: BOOT2_LOAD_BUF also determines where the btx loader and boot2.bin @@ -80,7 +119,93 @@ #define BOOT2_LOAD_BUF 0x8c00 /* boot1 loads boot2 */ #define MEM_BTX_ORG 0x9000 /* base of BTX code */ #define MEM_BTX_ENTRY 0x9010 /* BTX starts execution here */ +/* + * WARNING! The USR area may be messed around with in 16 bit code mode, + * data loaded should probably not cross 0xffff (e.g. boot2 loads + * ~8K at MEM_BTX_USR). + * + * MEM_BTX_USR is basically the segment offset BTX uses when + * running 'client' code. So address 0 in the client code will + * actually be physical address MEM_BTX_USR. + */ #define MEM_BTX_USR 0xa000 /* base of BTX client/user memory */ #define MEM_BTX_USR_ARG 0xa100 -#define MEM_BTX_LDR_OFF MEM_PAGE_SIZE /* offset of btx in the loader */ + +#elif defined(BOOT_NEWBOOTLOADER) && BOOT_NEWBOOTLOADER == 1 + +/************************************************************************ + * EXPERIMENTAL BOOTLOADER ADDRESS SET 1 * + ************************************************************************ + * + * + */ + +#define USR_ARGSPACE 0x1000 /* BTX loader / ttl argspace reserved */ +#define MEM_BTX_USR_STK 0x90000 /* (phys addr) btx client usr stack */ + +#define MEM_BTX_START 0x1000 /* (unchanged) */ +#define MEM_BTX_ESP0 0x1800 /* (unchanged) */ +#define MEM_BTX_BUF 0x1800 /* (unchanged) */ +#define MEM_BTX_ESP1 0x1e00 /* (unchanged) */ +#define MEM_BTX_IDT 0x1e00 /* (unchanged) */ +#define MEM_BTX_TSS 0x1f98 /* (unchanged) */ +#define MEM_BTX_MAP 0x2000 /* (unchanged) */ +#define MEM_BTX_DIR 0x4000 /* (unchanged) */ +#define MEM_BTX_TBL 0x5000 /* (unchanged) */ +#define MEM_BTX_ZEND 0x7000 /* (unchanged) */ + +#define MEM_BTX_ESP 0x7800 /* don't use 0x1000 */ +#define BOOT0_ORIGIN 0x7800 /* boot0 relocated */ +#define BOOT1_ORIGIN 0x7900 /* boot1 relocated (data only?) */ +#define MEM_ARG 0x7b00 /* cdboot/pxeboot disk/slice xfer */ +/******************** 0x7c00 BIOS LOAD ADDRESS (512 bytes) **********/ + +#define BOOT2_LOAD_BUF 0x8c00 /* (unchanged) */ +#define MEM_BTX_ORG 0x9000 /* (unchanged) */ +#define MEM_BTX_ENTRY 0x9010 /* (unchanged) */ + +#define MEM_BTX_USR 0xa000 /* (unchanged) */ +#define MEM_BTX_USR_ARG 0xa100 /* (unchanged) */ + + +#elif defined(BOOT_NEWBOOTLOADER) && BOOT_NEWBOOTLOADER == 2 + +/************************************************************************ + * EXPERIMENTAL BOOTLOADER ADDRESS SET 2 * + ************************************************************************ + * + * + */ +#define USR_ARGSPACE 0x1000 /* BTX loader / ttl argspace reserved */ +#define MEM_BTX_USR_STK 0x90000 /* (phys addr) btx client usr stack */ + +#define MEM_BTX_START 0x2000 /* (unchanged) */ +#define MEM_BTX_ESP0 0x2800 /* (unchanged) */ +#define MEM_BTX_BUF 0x2800 /* (unchanged) */ +#define MEM_BTX_ESP1 0x2e00 /* (unchanged) */ +#define MEM_BTX_IDT 0x2e00 /* (unchanged) */ +#define MEM_BTX_TSS 0x2f98 /* (unchanged) */ +#define MEM_BTX_MAP 0x3000 /* (unchanged) */ +#define MEM_BTX_DIR 0x5000 /* (unchanged) */ +/****** MEM_BTX_TBL (16K) SUPPORT REMOVED ***********************/ +#define MEM_BTX_ZEND 0x6000 /* (unchanged) */ + +#define MEM_BTX_ESP 0x7800 /* don't use 0x1000 */ +#define BOOT0_ORIGIN 0x7800 /* boot0 relocated */ +#define BOOT1_ORIGIN 0x7900 /* boot1 relocated (data only?) */ +#define MEM_ARG 0x7b00 /* cdboot/pxeboot disk/slice xfer */ +/******************** 0x7c00 BIOS LOAD ADDRESS (512 bytes) **********/ + +#define BOOT2_LOAD_BUF 0x8c00 /* (unchanged) */ +#define MEM_BTX_ORG 0x9000 /* (unchanged) */ +#define MEM_BTX_ENTRY 0x9010 /* (unchanged) */ + +#define MEM_BTX_USR 0xa000 /* base of BTX client/user memory */ +#define MEM_BTX_USR_ARG 0xa100 /* boot1->boot2 disk/slice xfer */ + +#else + +#error "BAD BOOT_NEWBOOTLOADER SETTING. UNSET TO GET DEFAULT" + +#endif /* BOOT_NEWBOOTLOADER */ diff --git a/sys/boot/pc32/bootasmdef.c b/sys/boot/pc32/bootasmdef.c new file mode 100644 index 0000000000..0554aa3930 --- /dev/null +++ b/sys/boot/pc32/bootasmdef.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $DragonFly: src/sys/boot/pc32/bootasmdef.c,v 1.1 2004/07/19 23:30:29 dillon Exp $ + */ + +#include +#include +#include +#include +#include "bootasm.h" + +static +void +usage(const char *arg0, int code) +{ + fprintf(stderr, "%s {BOOT0_ORIGIN,BOOT1_ORIGIN,MEM_BIOS_LADDR}\n", arg0); + exit(code); +} + +int +main(int ac, char **av) +{ + const char *fmt; + const char *var; + + if (ac == 1) + usage(av[0], 1); + if (strcmp(av[1], "-d") == 0) { + if (ac == 2) + usage(av[0], 1); + var = av[2]; + fmt = "%d\n"; + } else { + var = av[1]; + fmt = "0x%04x\n"; + } + + if (strcmp(var, "BOOT0_ORIGIN") == 0) { + printf(fmt, BOOT0_ORIGIN); + } else if (strcmp(var, "BOOT1_ORIGIN") == 0) { + printf(fmt, BOOT1_ORIGIN); + } else if (strcmp(var, "MEM_BIOS_LADDR") == 0) { + printf(fmt, MEM_BIOS_LADDR); + } else { + usage(av[0], 1); + } + return(0); +} + diff --git a/sys/boot/pc32/btx/btx/btx.S b/sys/boot/pc32/btx/btx/btx.S index 0a04d56e69..e7c1d6a725 100644 --- a/sys/boot/pc32/btx/btx/btx.S +++ b/sys/boot/pc32/btx/btx/btx.S @@ -1,4 +1,36 @@ /* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * * Copyright (c) 1998 Robert Nordier * All rights reserved. * @@ -13,7 +45,7 @@ * purpose. * * $FreeBSD: src/sys/boot/i386/btx/btx/btx.s,v 1.32 2002/10/08 18:19:02 jhb Exp $ - * $DragonFly: src/sys/boot/pc32/btx/btx/btx.S,v 1.6 2004/07/19 01:25:00 dillon Exp $ + * $DragonFly: src/sys/boot/pc32/btx/btx/btx.S,v 1.7 2004/07/19 23:30:33 dillon Exp $ */ #include "../../bootasm.h" @@ -81,6 +113,8 @@ /* * Code segment. + * + * BTX start. */ .globl start .code16 @@ -115,7 +149,7 @@ init: cli # Disable interrupts * Initialize memory. */ mov $MEM_BTX_IDT,%di # Memory to initialize - mov $(MEM_BTX_ORG-MEM_BTX_IDT)/2,%cx # Words to zero + mov $(MEM_BTX_ZEND-MEM_BTX_IDT)/2,%cx # Words to zero push %di # Save rep # Zero-fill stosw # memory @@ -217,18 +251,31 @@ init.8: xorl %ecx,%ecx # Zero */ movb $SEL_TSS,%cl # Set task ltr %cx # register + + /* + * BTX user area base of VM, for converting physical stack + * addresses to btx-client virtual stack addresses. + */ + movl $MEM_BTX_USR,%edx +#if !defined(MEM_BTX_USR_STK) /* * XXX We should NOT use BDA_MEM here. Use a fixed location - * instead. + * instead. (%eax is a physical stack addr) */ - movl $MEM_BTX_USR,%edx # User base address movzwl %ss:BDA_MEM,%eax # Get free memory shll $0xa,%eax # To bytes +#else + /* + * Use a fixed user stack instead of depending on BDA_MEM. + * %eax is a physical * stack address. + */ + movl $MEM_BTX_USR_STK,%eax +#endif subl $USR_ARGSPACE,%eax # Less arg space - subl %edx,%eax # Less base + subl %edx,%eax # Less base Phys->Virt movb $SEL_UDATA,%cl # User data selector pushl %ecx # Set SS - pushl %eax # Set ESP + pushl %eax # Set ESP (virtual address) push $0x202 # Set flags (IF set) push $SEL_UCODE # Set CS pushl btx_hdr+0xc # Set EIP @@ -236,7 +283,7 @@ init.8: xorl %ecx,%ecx # Zero pushl %ecx # Set FS pushl %ecx # Set DS pushl %ecx # Set ES - pushl %edx # Set EAX + pushl %edx # Set EAX (phys base addr of VM) movb $0x7,%cl # Set remaining init.9: push $0x0 # general loop init.9 # registers @@ -1127,6 +1174,21 @@ putchr.4: movw %dx,(%ebx) # Update position /* * Global descriptor table. + * + * 16: segment extent lsb + * 24: segment base lsb + * + * 5:TYPE + * 2:DPL + * 1:PRESENT + * + * 4: segment extent msb + * 2: unused + * 1: 32 bit, else 16 bit + * 1: limit granularity byte/page units + + * 8: segment base msb + * */ .p2align 4 gdt: .word 0x0,0x0,0x0,0x0 # Null entry diff --git a/sys/boot/pc32/btx/btxldr/Makefile b/sys/boot/pc32/btx/btxldr/Makefile index 52f78fc7c6..d29075e22b 100644 --- a/sys/boot/pc32/btx/btxldr/Makefile +++ b/sys/boot/pc32/btx/btxldr/Makefile @@ -1,8 +1,9 @@ # $FreeBSD: src/sys/boot/i386/btx/btxldr/Makefile,v 1.12 2003/06/26 03:51:57 peter Exp $ -# $DragonFly: src/sys/boot/pc32/btx/btxldr/Makefile,v 1.4 2004/06/25 00:38:38 dillon Exp $ +# $DragonFly: src/sys/boot/pc32/btx/btxldr/Makefile,v 1.5 2004/07/19 23:30:34 dillon Exp $ CFLAGS+= -DLOADER_ADDRESS=${LOADER_ADDRESS} +# BTXLDR_VERBOSE=YES .if defined(BTXLDR_VERBOSE) CFLAGS+= -DBTXLDR_VERBOSE .endif diff --git a/sys/boot/pc32/btx/btxldr/btxldr.S b/sys/boot/pc32/btx/btxldr/btxldr.S index ff8dd86365..a55484efb8 100644 --- a/sys/boot/pc32/btx/btxldr/btxldr.S +++ b/sys/boot/pc32/btx/btxldr/btxldr.S @@ -1,4 +1,36 @@ /* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * * Copyright (c) 1998 Robert Nordier * All rights reserved. * @@ -13,7 +45,7 @@ * purpose. * * $FreeBSD: src/sys/boot/i386/btx/btxldr/Makefile,v 1.17 2004/04/27 19:45:16 ru Exp $ - * $DragonFly: src/sys/boot/pc32/btx/btxldr/btxldr.S,v 1.2 2004/07/19 01:25:01 dillon Exp $ + * $DragonFly: src/sys/boot/pc32/btx/btxldr/btxldr.S,v 1.3 2004/07/19 23:30:34 dillon Exp $ */ /* @@ -50,6 +82,11 @@ .set SIZ_STUB,0x1a # Size of stub /* * We expect to be loaded by boot2 at the origin defined in ./Makefile. + * This is typically 0x200000. + * + * I *THINK* (not sure) that execution begins with us in 'virtual mode', + * meaning everything is offset by MEM_BTX_USR. We will load a gdt to + * set the base offsets back to 0. */ .globl start /* @@ -58,18 +95,29 @@ start: cld # String ops inc movl $m_logo,%esi # Identify call putstr # ourselves +#if !defined(MEM_BTX_USR_STK) movzwl BDA_MEM,%eax # Get base memory shll $0xa,%eax # Convert to bytes +#else + movl $MEM_BTX_USR_STK,%eax +#endif movl %eax,%ebp # Base of user stack #ifdef BTXLDR_VERBOSE movl $m_mem,%esi # Display call hexout # amount of call putstr # base memory #endif - lgdt gdtdesc # Load new GDT -/* - * Relocate caller's arguments. - */ + + /* + * Load a new GDT. XXX what does this do to running code + * segments? What if an interrupt occurs? What if the + * segment registers are reloaded? + */ + lgdt gdtdesc + + /* + * Relocate caller's arguments. + */ #ifdef BTXLDR_VERBOSE movl $m_esp,%esi # Display movl %esp,%eax # caller @@ -142,19 +190,33 @@ start_null_bi: movl $0x18,%ecx # Allocate space addb $'0',%al # Display call putchr # units call putstr # End message - movl %ebx,%esi # BTX image - movzwl 0x8(%ebx),%edi # Compute - orl $PAG_SIZ/PAG_ENT-1,%edi # the - incl %edi # BTX - shll $0x2,%edi # load - addl $MEM_BTX_TBL,%edi # address + + # Relocate the BTX image from wherever it was loaded (%ebx), + # which is typically offset 0x1000 in the load data, to + # MEM_BTX_ORG (typically 0x9000). + # + # MEM_BTX_TBL + ((mappages | 0x3ff) + 1) * 4 + # mappages is typically 0x0ffn so we get 0x1000*4 = 0x4000 + # MEM_BTX_TBL is traditionally mapped at 0x5000 so the + # whole calculation translated to MEM_BTX_ORG (0x9000). +#if 0 + /* XXX what is all of this junk? */ + movzwl 0x8(%ebx),%edi # Compute the BTX load address + orl $PAG_SIZ/PAG_ENT-1,%edi # (by skipping the page table) + incl %edi + shll $0x2,%edi + addl $MEM_BTX_TBL,%edi +#else + movl $MEM_BTX_ORG,%edi +#endif + movl %ebx,%esi # %esi = BTX image source pushl %edi # Save load address - movzwl 0xa(%ebx),%ecx # Image size + movzwl 0xa(%ebx),%ecx # Image size (bytes) #ifdef BTXLDR_VERBOSE pushl %ecx # Save image size #endif - rep # Relocate - movsb # BTX + rep # Relocate BTX + movsb movl %esi,%ebx # Keep place #ifdef BTXLDR_VERBOSE movl $m_rel_btx,%esi # Restore @@ -167,7 +229,12 @@ start_null_bi: movl $0x18,%ecx # Allocate space call hexout # relocation call putstr # message #endif - addl $PAG_SIZ,%ebp # Display + /* + * ADJUST EBP FOR USER BASE ADDRESS + * + * XXX why not just move MEM_BTX_USR into %ebp ? + */ + addl $MEM_BTX_USR-MEM_BTX_ORG,%ebp #ifdef BTXLDR_VERBOSE movl $m_base,%esi # the movl %ebp,%eax # user diff --git a/sys/boot/pc32/btx/lib/btxcsu.S b/sys/boot/pc32/btx/lib/btxcsu.S index 862af057e2..e6a7018dd9 100644 --- a/sys/boot/pc32/btx/lib/btxcsu.S +++ b/sys/boot/pc32/btx/lib/btxcsu.S @@ -13,11 +13,11 @@ * purpose. * * $FreeBSD: src/sys/boot/i386/btx/lib/btxcsu.s,v 1.3 1999/08/28 00:40:07 peter Exp $ - * $DragonFly: src/sys/boot/pc32/btx/lib/btxcsu.S,v 1.4 2004/07/19 01:25:02 dillon Exp $ + * $DragonFly: src/sys/boot/pc32/btx/lib/btxcsu.S,v 1.5 2004/07/19 23:30:35 dillon Exp $ */ /* - * BTX C startup code (ELF). + * BTX C startup code (ELF). Executed from ../btx/btx.S */ #include "../../bootasm.h" @@ -31,6 +31,7 @@ * Client entry point. */ _start: movl %eax,__base # Set base address + # for Virt->Phys conversions movl %esp,%eax # Get base of arguments addl $USR_ARGSPACE-USR_ARGOFFSET,%eax movl %eax,__args diff --git a/sys/boot/pc32/cdboot/cdboot.S b/sys/boot/pc32/cdboot/cdboot.S index ce7c404a8b..161a89e9ba 100644 --- a/sys/boot/pc32/cdboot/cdboot.S +++ b/sys/boot/pc32/cdboot/cdboot.S @@ -14,7 +14,7 @@ * * * $FreeBSD: src/sys/boot/i386/cdboot/cdboot.s,v 1.9 2001/11/07 01:20:33 jhb Exp $ - * $DragonFly: src/sys/boot/pc32/cdboot/cdboot.S,v 1.5 2004/07/18 23:40:07 dillon Exp $ + * $DragonFly: src/sys/boot/pc32/cdboot/cdboot.S,v 1.6 2004/07/19 23:30:36 dillon Exp $ */ /* @@ -248,7 +248,7 @@ pm_end: sti # Turn interrupts back on now * can see them. */ mov $MEM_ARG,%si # where the args are at now - mov $MEM_BTX_USR_ARG,%di # where the args are moving to + mov $MEM_BTX_USR_ARG,%di # where the args are moving to mov $(MEM_ARG_SIZE/4),%cx # size of the arguments in longs rep # Relocate movsl # the words diff --git a/sys/boot/pc32/libi386/amd64_tramp.S b/sys/boot/pc32/libi386/amd64_tramp.S index 9e77bc1fc6..71b65d2831 100644 --- a/sys/boot/pc32/libi386/amd64_tramp.S +++ b/sys/boot/pc32/libi386/amd64_tramp.S @@ -24,9 +24,11 @@ * SUCH DAMAGE. * * $FreeBSD: src/sys/boot/i386/libi386/amd64_tramp.S,v 1.2 2003/05/17 00:30:51 peter Exp $ - * $DragonFly: src/sys/boot/pc32/libi386/amd64_tramp.S,v 1.2 2003/11/22 19:30:55 asmodai Exp $ + * $DragonFly: src/sys/boot/pc32/libi386/amd64_tramp.S,v 1.3 2004/07/19 23:30:37 dillon Exp $ */ +#include "../bootasm.h" + /* * Quick and dirty trampoline to get into 64 bit (long) mode and running * with paging enabled so that we enter the kernel at its linked address. @@ -38,8 +40,7 @@ #define CR0_PG 0x80000000 /* GRRR. Deal with BTX that links us for a non-zero location */ -#define VPBASE 0xa000 -#define VTOP(x) ((x) + VPBASE) +#define VTOP(x) ((x) + MEM_BTX_USR) .data diff --git a/sys/boot/pc32/loader/Makefile b/sys/boot/pc32/loader/Makefile index ca98837081..02e2b675de 100644 --- a/sys/boot/pc32/loader/Makefile +++ b/sys/boot/pc32/loader/Makefile @@ -1,5 +1,6 @@ # $FreeBSD: src/sys/boot/i386/loader/Makefile,v 1.66 2003/06/26 03:51:57 peter Exp $ -# $DragonFly: src/sys/boot/pc32/loader/Makefile,v 1.5 2004/01/28 19:24:14 dillon Exp $ +# $DragonFly: src/sys/boot/pc32/loader/Makefile,v 1.6 2004/07/19 23:30:38 dillon Exp $ +# PROG= loader STRIP= @@ -7,6 +8,14 @@ NEWVERSWHAT= "bootstrap loader" i386 BINDIR?= /boot INSTALLFLAGS= -b +# Notes: the 'loader' client is brought in with a virtual base of (typically) +# 0xa000 and a code offset of 0x0. The btxldr is loaded at 0x200000 (?). +# +# MEM_BTX_USR_ARG is not used in the boot2->loader stage, only in the +# boot1->boot2 stage (XXX what about the use in cdboot and pxeboot?) +# +ORG= 0x0 + # architecture-specific loader code SRCS= main.c conf.c @@ -49,7 +58,7 @@ CFLAGS+= -I${.CURDIR}/../../.. -I. CLEANFILES+= vers.c vers.o ${PROG}.list ${PROG}.bin ${PROG}.sym ${PROG}.help CFLAGS+= -Wall -LDFLAGS= -nostdlib -static -Ttext 0x0 +LDFLAGS= -nostdlib -static -Ttext ${ORG} # i386 standalone support library LIBI386= ${.OBJDIR}/../libi386/libi386.a diff --git a/sys/boot/pc32/loader/main.c b/sys/boot/pc32/loader/main.c index 66fa97ec2b..edc3a6713b 100644 --- a/sys/boot/pc32/loader/main.c +++ b/sys/boot/pc32/loader/main.c @@ -1,4 +1,36 @@ -/*- +/* + * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * * Copyright (c) 1998 Michael Smith * All rights reserved. * @@ -24,7 +56,7 @@ * SUCH DAMAGE. * * $FreeBSD: src/sys/boot/i386/loader/main.c,v 1.28 2003/08/25 23:28:32 obrien Exp $ - * $DragonFly: src/sys/boot/pc32/loader/main.c,v 1.6 2004/06/26 22:37:11 dillon Exp $ + * $DragonFly: src/sys/boot/pc32/loader/main.c,v 1.7 2004/07/19 23:30:38 dillon Exp $ */ /* @@ -72,6 +104,7 @@ extern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[]; /* XXX debugging */ extern char end[]; +#define COMCONSOLE_DEBUG #ifdef COMCONSOLE_DEBUG static void @@ -98,10 +131,12 @@ WDEBUG(char c) #endif +static void nop(void) { } int main(void) { - int i; + char *memend; + int i; WDEBUG_INIT(); WDEBUG('X'); @@ -119,15 +154,28 @@ main(void) /* * Initialize the heap as early as possible. Once this is done, - * malloc() is usable. + * malloc() is usable. + * + * Don't include our stack in the heap. If the stack is in low + * user memory use {end,bios_basemem}. If the stack is in high + * user memory but not extended memory then don't let the heap + * overlap the stack. If the stack is in extended memory limit + * the heap to bios_basemem. + * + * Be sure to use the virtual bios_basemem address rather then + * the physical bios_basemem address or we may overwrite BIOS + * data. */ bios_getmem(); -#if 0 /* FUTURE */ - if (bios_basemem > 0x9f000) - bios_basemem = 0x9f000; -#endif - - setheap((void *)end, (void *)bios_basemem); + memend = (char *)&memend - 0x8000; /* space for stack */ + memend = (char *)((uintptr_t)memend & ~(uintptr_t)(0x1000 - 1)); + if (memend < (char *)end) { + setheap((void *)end, PTOV(bios_basemem)); + } else { + if (memend > (char *)PTOV(bios_basemem)) + memend = (char *)PTOV(bios_basemem); + setheap((void *)end, memend); + } /* * XXX Chicken-and-egg problem; we want to have console output early,