diff --git a/.gitignore b/.gitignore index 503c8d8..84f7aae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.o +*.d main attach_gdb_to_this main diff --git a/Makefile b/Makefile index ec5c655..6278092 100644 --- a/Makefile +++ b/Makefile @@ -6,11 +6,15 @@ OBJCOPY=m68k-elf-objcopy QEMU=/opt/m68k/bin/qemu-system-m68k CFLAGS=-nostdlib -nostartfiles -nodefaultlibs +CFLAGS+=-Wall -Werror CFLAGS+=-m68000 CFLAGS+=-Os -main: assembly.o iv.o screen.o main.o exception.o linker.x kmem.o string.o - ${LD} main.o assembly.o iv.o screen.o exception.o kmem.o string.o -o main -T linker.x -Map main.map +SOURCES=assembly.c screen.c main.c exception.c kmem.c string.c +OBJECTS=$(subst .c,.o,$(SOURCES)) + +main: ${OBJECTS} iv.o linker.x + ${LD} ${OBJECTS} iv.o -o main -T linker.x -Map main.map cp main attach_gdb_to_this ${OBJCOPY} -O srec main @@ -22,20 +26,15 @@ disassemble: main iv.o: iv.asm ${AS} -o iv.o iv.asm -main.o: main.c - ${CC} ${CFLAGS} -c main.c -o main.o - -assembly.o: assembly.h assembly.c - ${CC} ${CFLAGS} -o assembly.o -c assembly.c - -screen.o: screen.h screen.c assembly.h - ${CC} ${CFLAGS} -o screen.o -c screen.c - -exception.o: exception.c - ${CC} ${CFLAGS} -o exception.o -c exception.c +%.o: %.c + ${CC} ${CFLAGS} -o $@ -c $< +%.d: %.c + $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ + sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ + rm -f $@.$$$$ -kmem.o: kmem.h kmem.c - ${CC} -o kmem.o -c kmem.c +DEPENDS=$(subst .c,.d,$(SOURCES)) +-include ${DEPENDS} run: main ${QEMU} -M cecs -nographic -kernel main -gdb tcp::1234 @@ -44,5 +43,5 @@ debug: main ${QEMU} -M cecs -nographic -kernel main -S -gdb tcp::1234 clean: - rm assembly.o screen.o iv.o attach_gdb_to_this main main.o kmem.o main.map string.o + rm iv.o attach_gdb_to_this main main.map ${DEPENDS} ${OBJECTS} diff --git a/iv.asm b/iv.asm index bb2b9d6..01fcd0f 100644 --- a/iv.asm +++ b/iv.asm @@ -1,9 +1,6 @@ -.data - .org 0x0 - dc.l __asm_stack__,main .equ __asm_stack__,0x4400 .section .text - dc.l __asm_stack__,main + dc.l __asm_stack__,entry dc.l excep_undef dc.l excep_undef /* 4 */ diff --git a/kmem.c b/kmem.c index 1aedbf8..60cc497 100644 --- a/kmem.c +++ b/kmem.c @@ -8,7 +8,7 @@ char *blocks; void kfree(void *ptr) { ptr = ptr - 4; - int size = *((int *)ptr); + int __attribute__ ((unused)) size = *((int *)ptr); int index = (((int) ptr) - MEM_START)/MEM_BLOCK_SIZE; CLRBIT(index); *((int *)ptr) = 0; @@ -53,7 +53,7 @@ void *kmalloc(int size) { void kmeminit() { int needed_blocks = (MEM_BITSET_BYTES+MEM_BLOCK_SIZE-1)/MEM_BLOCK_SIZE; int i; - blocks = (int *)MEM_START; + blocks = (char *)MEM_START; for (i = 0; i < needed_blocks; i++) SETBIT(i); for (; i < MEM_BITSET_BITS; i++) diff --git a/linker.x b/linker.x index bfc852f..2a35952 100644 --- a/linker.x +++ b/linker.x @@ -9,12 +9,16 @@ SECTIONS { .text : { * (.text*); * (.rodata*); - etext = .; /*End of test section*/ } >ROM /* The C code needs to reference all initialized data at its RAM address, but we want it to be writable. This causes the .data section to be physically located after the .text section in ROM, but referenced at its RAM address. Code must copy this data section to RAM at boot. */ - .data : AT (etext) { * (.data); } >RAM + .data : { + data_load = LOADADDR(.data); + data_start = .; + * (.data); + data_end = .; + } >RAM AT>ROM } diff --git a/main.c b/main.c index 97953bf..ba407e7 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,30 @@ #include "assembly.h" #include "screen.h" #include "kmem.h" +#include "string.h" + +extern unsigned char data_load; +extern unsigned char data_start; +extern unsigned char data_end; + +int foo = 0xdeadbeef; + +/* Copy the .data section from ROM to RAM */ +static void copy_data(void) +{ + unsigned char *rom_data = &data_load; + unsigned char *ram_data = &data_start; + + if (foo == 0xdeadbeef) + putstr("Warning: .data appears initialized before copying\n"); + + /* Could be replaced with a call to memcpy */ + while (ram_data < &data_end) { + *(ram_data++) = *(rom_data++); + } + if (foo != 0xdeadbeef) + putstr("Warning: .data not initialized as expected\n"); +} void *test_malloc(int bytes) { void *ptr = kmalloc(bytes); @@ -19,15 +43,16 @@ void test_free(void *ptr) { void memtest() { void *p1 = test_malloc(124); - void *p2 = test_malloc(124); + void __attribute__ ((unused)) *p2 = test_malloc(124); test_free(p1); - void *p3 = test_malloc(124); + void __attribute__ ((unused)) *p3 = test_malloc(124); } void shell() { char ptr[64]; + copy_data(); putstr("Welcome to the (unstable) C Kernel\n"); while(1) @@ -57,7 +82,7 @@ void shell() { } } -void main() { +void entry() { __asm_initialize__(); kmeminit();