From 86ef52d03267817a0442f2aa340860871ddb9e63 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sat, 28 Aug 2004 07:08:54 +0000 Subject: [PATCH] Work on loader to prepare for passing in a command line. --- src/Makefile.inc | 7 +++--- src/pad | 20 +++++++++++++++++ src/threads/init.c | 2 +- src/threads/loader.S | 53 ++++++++++++++++++++++++++++++-------------- 4 files changed, 60 insertions(+), 22 deletions(-) create mode 100755 src/pad diff --git a/src/Makefile.inc b/src/Makefile.inc index 6ed310e..6ffa350 100644 --- a/src/Makefile.inc +++ b/src/Makefile.inc @@ -54,16 +54,15 @@ OBJECTS = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(SOURCES))) all: diskimage.bin intr-stubs.S: $(TOP_SRCDIR)/threads/intr-stubs.pl - $< > $@.tmp && mv $@.tmp $@ + $< > $@ kernel.o: $(OBJECTS) ld -T $(TOP_SRCDIR)/threads/kernel.lds -o $@ $^ \ `$(CC) -print-libgcc-file-name` kernel.bin: kernel.o - objcopy -O binary -R .note -R .comment -S $< $@.data - perl -e 'print "\0" x (4096 - (-s "$@.data") % 4096)' > $@.pad - cat $@.data $@.pad > $@.tmp && mv $@.tmp $@ + objcopy -O binary -R .note -R .comment -S $< $@.tmp + $(TOP_SRCDIR)/pad 4096 < $@.tmp > $@ loader.bin: loader.S kernel.bin gcc -c $< -DKERNEL_LOAD_PAGES=`perl -e 'print +(-s "kernel.bin") / 4096;'` diff --git a/src/pad b/src/pad new file mode 100755 index 0000000..82f35d3 --- /dev/null +++ b/src/pad @@ -0,0 +1,20 @@ +#! /usr/bin/perl + +if (@ARGV != 1) { + print STDERR "pad, for padding a file out to an even block size\n"; + print STDERR "usage: pad BLOCKSIZE < INPUT > OUTPUT\n\n"; + print STDERR "pad copies its input to its output, then writes\n"; + print STDERR "zeroes to its output as necessary to make the\n"; + print STDERR "output size an even multiple of BLOCKSIZE bytes\n"; + exit 1; +} + +$blocksize = $ARGV[0]; +$input_size = 0; +while (read (STDIN, $input, 4096)) { + $input_size += length ($input); + print $input; +} + +$odd_bytes = $input_size % $blocksize; +print "\0" x ($blocksize - $odd_bytes) if $odd_bytes != 0; diff --git a/src/threads/init.c b/src/threads/init.c index f247612..6da714e 100644 --- a/src/threads/init.c +++ b/src/threads/init.c @@ -50,7 +50,7 @@ main (void) /* Calculate how much RAM the kernel uses, and find out from the bootloader how much RAM this machine has. */ kernel_pages = (&_end - &_text + 4095) / 4096; - ram_pages = *(uint32_t *) (0x7e00 - 8); + ram_pages = *(uint32_t *) (0x7e00 - 6); printk ("Initializing nachos-x86, %d kB RAM detected.\n", ram_pages * 4); diff --git a/src/threads/loader.S b/src/threads/loader.S index c60e8dd..fd8285a 100644 --- a/src/threads/loader.S +++ b/src/threads/loader.S @@ -1,14 +1,15 @@ #include "mmu.h" -################################################################################### -# ENTRY POINT -# This code should be stored in the first sector of the hard disk. When the -# BIOS runs, it loads this code at physical address 0x7c00 - 0x7e00 (512 bytes). -# Then jumps to the beginning of it, in real-mode (BIOS runs in real mode). -# +############################################################################## +# Kernel loader. +# +# This code should be stored in the first sector of the hard disk. When the +# BIOS runs, it loads this code at physical address 0x7c00-0x7e00 (512 bytes). +# Then it jumps to the beginning of it, in real mode. # This code switches into protected mode (32-bit mode) so that all of -# memory can accessed, then calls into C. -################################################################################### +# memory can accessed, loads the kernel into memory, and jumps to the +# first byte of the kernel, where start.S is linked. +############################################################################## .globl start # Entry point start: .code16 # This runs in real mode @@ -40,12 +41,12 @@ start: .code16 # This runs in real mode #### Returns CF clear if successful, with AX = (kB of physical memory) - 1024. #### This only works for memory sizes <= 65 MB, which should be fine for our purposes. - movb $0x88,%ah - int $0x15 - jc panic # Carry flag set on error - addl $1024,%eax # Total kB - shrl $2,%eax # Total 4 kB pages - movl %eax, ram_pages + movb $0x88,%ah + int $0x15 + jc panic # Carry flag set on error +2: addl $1024,%eax # Total kB + shrl $2,%eax # Total 4 kB pages + movl %eax, ram_pages #### switch from real to protected mode #### The segments in GDT allow all of physical memory to be accessed. @@ -178,11 +179,29 @@ gdtdesc: .word 0x17 # sizeof (gdt) - 1 .long gdt # address gdt -##### Arrive here on error -panic: jmp panic +##### To panic, we print panicmsg (with help from the BIOS) and spin. +panic: .code16 # We only panic in real mode. + movw $panicmsg, %si + movb $0xe, %ah + xorb %bh, %bh +1: lodsb + test %al, %al +2: jz 2b # Spin. + int $0x10 + jmp 1b + +panicmsg: + .ascii "Loader panic!\r\n" + .byte 0 + +##### Command-line arguments inserted by another utility. +##### The loader doesn't use these, but we note their +##### location here for easy reference. + .org 0x200 - 0x80 +cmd_line: ##### Memory size in 4 kB pages. - .org 0x200 - 8 + .org 0x200 - 6 ram_pages: .long 0 -- 2.30.2