Browse Source

Restructure repository.

Giovanni Mascellani 5 years ago
parent
commit
2f52e1564b

+ 71 - 70
Makefile

@@ -1,101 +1,102 @@
 
 AR=ar
 
-all: asmasm asmasm.x86 boot.iso cc asmg boot.x86
+all: build build/asmasm_linux build/boot_asmasm.x86 build/boot_empty.x86 build/boot_asmg.x86 build/boot.iso
 
-asmasm.o: asmasm.asm library.asm stub.asm
-	nasm -f elf -F dwarf -g -w-number-overflow -o asmasm.o stub.asm
+# Trivial things
+build:
+	mkdir build
 
-staging.o: staging.c platform.h
-	gcc -m32 -c -Og -g -ffreestanding staging.c -o staging.o
+build/zero_sect.bin:
+	dd if=/dev/zero bs=512 count=1 of=$@
 
-platform.o: platform.c platform.h
-	gcc -m32 -c -Og -g -ffreestanding platform.c -o platform.o
+build/END:
+	touch $@
 
-platform_asm.o: platform_asm.asm
-	nasm -f elf -F dwarf -g platform_asm.asm
+# Bootloader
+build/bootloader.x86.exe: boot/bootloader.asm lib/atapio.asm
+	nasm -f bin -I lib/ -o $@ $<
 
-asmasm: asmasm.o staging.o platform.o platform_asm.o
-	gcc -m32 -O0 -g -o asmasm asmasm.o staging.o platform.o platform_asm.o
+# Asmasm executable
+build/asmasm_linux.asm: asmasm/asmasm_linux.asm lib/library.asm asmasm/asmasm.asm
+	cat $^ > $@
 
-full-asmasm.asm: kernel.asm ar.asm library.asm asmasm.asm kernel-asmasm.asm top.asm
-	cat kernel.asm ar.asm library.asm asmasm.asm kernel-asmasm.asm top.asm | grep -v "^ *section " | grep -v "^ *bits " | grep -v "^ *org " > full-asmasm.asm
+build/asmasm_linux.o: build/asmasm_linux.asm
+	nasm -f elf -F dwarf -g -w-number-overflow -o $@ $<
 
-END:
-	touch END
+build/platform_linux.o: lib/platform_linux.c lib/platform.h
+	gcc -m32 -c -Og -g -Ilib -o $@ $<
 
-initrd-asmasm.ar: main.asm atapio.asm END
-	-rm initrd-asmasm.ar
-	$(AR) rcs initrd-asmasm.ar main.asm atapio.asm END
+build/asmasm_linux: build/asmasm_linux.o build/platform_linux.o
+	gcc -m32 -Og -g -o $@ $^
 
-asmasm.x86.exe: full-asmasm.asm asmasm
-	./asmasm full-asmasm.asm > asmasm.x86.exe
+build/boot_asmasm.x86: build/bootloader.x86.exe build/asmasm.x86 build/zero_sect.bin
+	cat $^ > $@
 
-asmasm.x86: asmasm.x86.exe initrd-asmasm.ar
-	cat asmasm.x86.exe initrd-asmasm.ar > asmasm.x86
+# Asmasm kernel
+build/full-asmasm.asm: lib/kernel.asm lib/ar.asm lib/library.asm asmasm/asmasm.asm asmasm/kernel-asmasm.asm lib/top.asm
+	cat $^ | grep -v "^ *bits " | grep -v "^ *org " > $@
 
-boot/boot/grub/grub.cfg: grub.cfg
-	mkdir -p boot/boot/grub
-	cp grub.cfg boot/boot/grub
+build/initrd-asmasm.ar: asmasm/main.asm lib/atapio.asm build/END
+	-rm $@
+	$(AR) rcs $@ $^
 
-boot/boot/asmasm.x86: asmasm.x86
-	mkdir -p boot/boot
-	cp asmasm.x86 boot/boot
+build/asmasm.x86.exe: build/full-asmasm.asm build/asmasm_linux
+	./build/asmasm_linux $< > $@
 
-full-empty.asm: kernel.asm ar.asm library.asm kernel-empty.asm top.asm
-	cat kernel.asm ar.asm library.asm kernel-empty.asm top.asm | grep -v "^ *section " | grep -v "^ *bits " | grep -v "^ *org " > full-empty.asm
+build/asmasm.x86: build/asmasm.x86.exe build/initrd-asmasm.ar
+	cat $^ > $@
 
-initrd-empty.ar: END
-	-rm initrd-empty.ar
-	$(AR) rcs initrd-empty.ar END
+# Empty kernel
+build/full-empty.asm: lib/kernel.asm lib/ar.asm lib/library.asm empty/kernel-empty.asm lib/top.asm
+	cat $^ | grep -v "^ *bits " | grep -v "^ *org " > $@
 
-empty.x86.exe: full-empty.asm asmasm
-	./asmasm full-empty.asm > empty.x86.exe
+build/initrd-empty.ar: build/END
+	-rm $@
+	$(AR) rcs $@ $^
 
-empty.x86: empty.x86.exe initrd-empty.ar
-	cat empty.x86.exe initrd-empty.ar > empty.x86
+build/empty.x86.exe: build/full-empty.asm build/asmasm_linux
+	./build/asmasm_linux $< > $@
 
-boot/boot/empty.x86: empty.x86
-	mkdir -p boot/boot
-	cp empty.x86 boot/boot
+build/empty.x86: build/empty.x86.exe build/initrd-empty.ar
+	cat $^ > $@
 
-boot.iso: boot/boot/grub/grub.cfg boot/boot/asmasm.x86 boot/boot/empty.x86 boot/boot/asmg.x86
-	grub-mkrescue -o boot.iso boot
+build/boot_empty.x86: build/bootloader.x86.exe build/empty.x86 build/zero_sect.bin
+	cat $^ > $@
 
-cc: cc.c
-	gcc -m32 -O0 -fwrapv -g -o cc cc.c
+# Asmg kernel
+build/full-asmg.asm: lib/kernel.asm lib/ar.asm lib/library.asm asmg/asmg.asm asmg/kernel-asmg.asm lib/top.asm
+	cat $^ | grep -v "^ *bits " | grep -v "^ *org " > $@
 
-asmg.o: asmg.asm gstub.asm library.asm
-	nasm -f elf -F dwarf -g -w-number-overflow -o asmg.o gstub.asm
+build/initrd-asmg.ar: asmg/*.g test/test.c test/first.h test/other.h build/END
+	-rm $@
+	$(AR) rcs $@ $^
 
-gstaging.o: gstaging.c platform.h
-	gcc -m32 -c -Og -g -ffreestanding gstaging.c -o gstaging.o
+build/asmg.x86.exe: build/full-asmg.asm build/asmasm_linux
+	./build/asmasm_linux $< > $@
 
-asmg: asmg.o gstaging.o platform.o platform_asm.o
-	gcc -m32 -O0 -g -o asmg asmg.o gstaging.o platform.o platform_asm.o
+build/asmg.x86: build/asmg.x86.exe build/initrd-asmg.ar
+	cat $^ > $@
 
-full-asmg.asm: kernel.asm ar.asm library.asm kernel-asmg.asm asmg.asm top.asm
-	cat kernel.asm ar.asm library.asm kernel-asmg.asm asmg.asm top.asm | grep -v "^ *section " > full-asmg.asm
+build/boot_asmg.x86: build/bootloader.x86.exe build/asmg.x86 build/zero_sect.bin
+	cat $^ > $@
 
-initrd-asmg.ar: main.g test.c first.h other.h utils.g simple_malloc.g triv_malloc.g vector.g map.g preproc.g ast.g END
-	-rm initrd-asmg.ar
-	$(AR) rcs initrd-asmg.ar main.g test.c first.h other.h utils.g simple_malloc.g triv_malloc.g vector.g map.g preproc.g ast.g END
+# GRUB ISO image
+build/boot/boot/grub/grub.cfg: boot/grub.cfg
+	mkdir -p build/boot/boot/grub
+	cp $^ $@
 
-asmg.x86.exe: full-asmg.asm
-	nasm -f bin full-asmg.asm -o asmg.x86.exe
+build/boot/boot/asmasm.x86: build/asmasm.x86
+	mkdir -p build/boot/boot
+	cp $^ $@
 
-asmg.x86: asmg.x86.exe initrd-asmg.ar
-	cat asmg.x86.exe initrd-asmg.ar > asmg.x86
+build/boot/boot/empty.x86: build/empty.x86
+	mkdir -p build/boot/boot
+	cp $^ $@
 
-boot/boot/asmg.x86: asmg.x86
-	mkdir -p boot/boot
-	cp asmg.x86 boot/boot
+build/boot/boot/asmg.x86: build/asmg.x86
+	mkdir -p build/boot/boot
+	cp $^ $@
 
-bootloader.x86.exe: bootloader.asm
-	nasm bootloader.asm -f bin -o bootloader.x86.exe
-
-zero_sect.bin:
-	dd if=/dev/zero bs=512 count=1 of=zero_sect.bin
-
-boot.x86: bootloader.x86.exe asmg.x86 zero_sect.bin
-	cat bootloader.x86.exe asmg.x86 zero_sect.bin > boot.x86
+build/boot.iso: build/boot/boot/grub/grub.cfg build/boot/boot/asmasm.x86 build/boot/boot/empty.x86 build/boot/boot/asmg.x86
+	cd build && grub-mkrescue -o boot.iso boot

+ 0 - 0
asmasm.asm → asmasm/asmasm.asm


+ 35 - 0
asmasm/asmasm_linux.asm

@@ -0,0 +1,35 @@
+
+  extern platform_panic
+  extern platform_exit
+  extern platform_open_file
+  extern platform_reset_file
+  extern platform_read_char
+  extern platform_write_char
+  extern platform_log
+  extern platform_allocate
+
+  section .data
+
+  global main
+main:
+  ;; Init everything
+  call init_symbols
+  call init_assembler
+
+  ;; Open input file
+  mov eax, [esp+8]
+  add eax, 4
+  mov eax, [eax]
+  push eax
+  call platform_open_file
+  add esp, 4
+
+  ;; Call assemble
+  push 0x100000
+  push 1
+  push eax
+  call assemble
+  add esp, 12
+
+  mov eax, 0
+  ret

+ 0 - 0
kernel-asmasm.asm → asmasm/kernel-asmasm.asm


+ 0 - 0
main.asm → asmasm/main.asm


+ 0 - 0
asmg.asm → asmg/asmg.asm


+ 0 - 0
ast.g → asmg/ast.g


+ 0 - 0
kernel-asmg.asm → asmg/kernel-asmg.asm


+ 0 - 0
main.g → asmg/main.g


+ 0 - 0
map.g → asmg/map.g


+ 0 - 0
preproc.g → asmg/preproc.g


+ 0 - 0
simple_malloc.g → asmg/simple_malloc.g


+ 0 - 0
test.g → asmg/test.g


+ 0 - 0
triv_malloc.g → asmg/triv_malloc.g


+ 0 - 0
utils.g → asmg/utils.g


+ 0 - 0
vector.g → asmg/vector.g


+ 0 - 0
gstaging.c → attic/gstaging.c


+ 0 - 0
gstub.asm → attic/gstub.asm


+ 0 - 0
platform_asm.asm → attic/platform_asm.asm


+ 0 - 0
staging.c → attic/staging.c


+ 0 - 0
stub.asm → attic/stub.asm


+ 0 - 0
bootloader.asm → boot/bootloader.asm


+ 0 - 0
grub.cfg → boot/grub.cfg


+ 0 - 0
cc.c → cc/cc.c


+ 0 - 0
cc2.c → cc/cc2.c


+ 0 - 0
kernel-empty.asm → empty/kernel-empty.asm


+ 0 - 0
ar.asm → lib/ar.asm


+ 0 - 0
atapio.asm → lib/atapio.asm


+ 0 - 0
kernel.asm → lib/kernel.asm


+ 0 - 0
library.asm → lib/library.asm


+ 0 - 0
platform.h → lib/platform.h


+ 60 - 0
lib/platform_linux.c

@@ -0,0 +1,60 @@
+
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "platform.h"
+
+__attribute__((noreturn)) void platform_panic() {
+  abort();
+}
+
+void platform_exit() {
+  exit(0);
+}
+
+int platform_open_file(char *fname) {
+  int ret = open(fname, O_RDONLY);
+  if (ret < 0) {
+    platform_panic();
+  }
+  return ret;
+}
+
+int platform_reset_file(int fd) {
+  int res = lseek(fd, 0, SEEK_SET);
+  if (res < 0) {
+    platform_panic();
+  }
+}
+
+int platform_read_char(int fd) {
+  int buf = 0;
+  int ret = read(fd, &buf, 1);
+  if (ret == 0) {
+    return -1;
+  }
+  if (ret != 1) {
+    platform_panic();
+  }
+  return buf;
+}
+
+void platform_write_char(int fd, int c) {
+  int buf = c;
+  int ret = write(fd, &buf, 1);
+  if (ret != 1) {
+    platform_panic();
+  }
+}
+
+void platform_log(int fd, char *s) {
+  while (*s != '\0') {
+    platform_write_char(fd, *s);
+    s++;
+  }
+}
+
+void *platform_allocate(int size) {
+  return malloc(size);
+}

+ 0 - 0
top.asm → lib/top.asm


+ 0 - 89
platform.c

@@ -1,89 +0,0 @@
-
-#include "platform.h"
-
-int do_syscall(int syscall_num, int arg1, int arg2, int arg3);
-
-#define SYS_exit 0x1
-#define SYS_read 0x3
-#define SYS_write 0x4
-#define SYS_getpid 0x14
-#define SYS_kill 0x25
-#define SYS_open 0x5
-#define SYS_lseek 0x13
-#define SYS_brk 0x2d
-
-#define SIGABRT 6
-
-#define O_RDONLY 0
-#define SEEK_SET 0
-
-__attribute__((noreturn)) void platform_panic() {
-  int pid = do_syscall(SYS_getpid, 0, 0, 0);
-  do_syscall(SYS_kill, pid, SIGABRT, 0);
-  __builtin_unreachable();
-}
-
-void platform_exit() {
-  do_syscall(SYS_exit, 0, 0, 0);
-  __builtin_unreachable();
-}
-
-int platform_open_file(char *fname) {
-  int ret = do_syscall(SYS_open, (int) fname, O_RDONLY, 0);
-  if (ret < 0) {
-    platform_panic();
-  }
-  return ret;
-}
-
-int platform_reset_file(int fd) {
-  int res = do_syscall(SYS_lseek, fd, 0, SEEK_SET);
-  if (res < 0) {
-    platform_panic();
-  }
-}
-
-int platform_read_char(int fd) {
-  int buf = 0;
-  int ret = do_syscall(SYS_read, fd, (int) &buf, 1);
-  if (ret == 0) {
-    return -1;
-  }
-  if (ret != 1) {
-    platform_panic();
-  }
-  return buf;
-}
-
-void platform_write_char(int fd, int c) {
-  int buf = c;
-  int ret = do_syscall(SYS_write, fd, (int) &buf, 1);
-  if (ret != 1) {
-    platform_panic();
-  }
-}
-
-void platform_log(int fd, char *s) {
-  while (*s != '\0') {
-    platform_write_char(fd, *s);
-    s++;
-  }
-}
-
-char *itoa(int x);
-
-void *platform_allocate(int size) {
-  int current_brk = do_syscall(SYS_brk, 0, 0, 0);
-  int new_brk = current_brk + size;
-  new_brk = 1 + ((new_brk - 1) | 0xf);
-  int returned_brk = do_syscall(SYS_brk, new_brk, 0, 0);
-  if (returned_brk != new_brk) {
-    platform_panic();
-  }
-  /*platform_log(2, "Allocate from ");
-  platform_log(2, itoa(current_brk));
-  platform_log(2, " to ");
-  platform_log(2, itoa(new_brk));
-  platform_log(2, "\n");*/
-  return (void*) current_brk;
-}

+ 0 - 0
first.h → test/first.h


+ 0 - 0
other.h → test/other.h


+ 0 - 0
test.c → test/test.c