Procházet zdrojové kódy

evx: starting kernel on i386

rain před 5 roky
rodič
revize
6dfe5aa908

+ 20 - 3
Makefile

@@ -23,14 +23,22 @@ KERNEL_VERSION 		= $(KERNEL_MAJOR).$(KERNEL_PATCH_LEVEL).$(KERNEL_SUBVER)\
 			$(KERNEL_COMPILE_TIME) $(KERNEL_COMPILE_BY) on $(ARCH)
 			$(KERNEL_COMPILE_TIME) $(KERNEL_COMPILE_BY) on $(ARCH)
 
 
 MAKEFLAGS		+= --no-print-directory 
 MAKEFLAGS		+= --no-print-directory 
+CFLAGS 			+= -I$(PWD)/include/ -g -nostdinc -nostdlib 
+ARCHCFLAGS 		+= $(shell cat $(ARCHDIR)/.archcflags)
 
 
 SHELl 			= $(shell echo $$SHELL)
 SHELl 			= $(shell echo $$SHELL)
-EXPORT 			= export
+
+TARGET 			= vmevx.bin 
+
 
 
 ifeq 	($(CROSS_COMPILE), )
 ifeq 	($(CROSS_COMPILE), )
 	CROSS_COMPILE	= $(CC)
 	CROSS_COMPILE	= $(CC)
 endif
 endif
 
 
+ifeq 	($(CROSS_LD), )
+	CROSS_LD = $(LD)
+endif
+
 
 
 ifeq 	($(ARCH), x86_64) 
 ifeq 	($(ARCH), x86_64) 
 	ARCH		= x86
 	ARCH		= x86
@@ -42,25 +50,34 @@ ifeq 	($D,)
 
 
 # that inside object, and we don't support other objects at this time :-)
 # that inside object, and we don't support other objects at this time :-)
 obj-i 			+= $(PWD)/kernel/ 
 obj-i 			+= $(PWD)/kernel/ 
+obj-i 			+= $(PWD)/init/ 
+
 obj-i 			+= $(ARCHDIR)/ 
 obj-i 			+= $(ARCHDIR)/ 
+obj-i 			+= $(ARCHDIR)/init/ 
 
 
 
 
 _all:
 _all:
+	@rm .objlist -f  
 	@for d in $(obj-i); do 				\
 	@for d in $(obj-i); do 				\
 		$(MAKE) $(MAKEFLAGS) -C $$d D=$$d;	\
 		$(MAKE) $(MAKEFLAGS) -C $$d D=$$d;	\
 	done
 	done
+	@$(MAKE) -C $(ARCHDIR)/ vmevx.o
+	@$(MAKE) -C $(ARCHDIR)/ boot 
+
 
 
 
 
 .PHONY clean:
 .PHONY clean:
 	@find ./ -name "*.o" \
 	@find ./ -name "*.o" \
 	       -o -name "*.bin"	| xargs rm -f
 	       -o -name "*.bin"	| xargs rm -f
 	@$(MAKE) -C $(ARCHDIR)/ clean
 	@$(MAKE) -C $(ARCHDIR)/ clean
+	@rm .objlist 
 
 
 else
 else
 
 
 __all: 	
 __all: 	
-	@for d in $(obj-i); do 								\
-		$(SHELL) scripts/do_build.sh $$d $(CROSS_COMPILE) $D $(CFLAGS);		\
+	@for d in $(obj-i); do 										\
+		$(SHELL) scripts/do_build.sh $$d $(CROSS_COMPILE) $D "$(CFLAGS)" "$(ARCHCFLAGS)";	\
+		echo $D/$$d.o >> .objlist;								\
 	done
 	done
 
 
 endif
 endif

+ 1 - 0
arch/x86/.archcflags

@@ -0,0 +1 @@
+-m32

+ 12 - 2
arch/x86/Makefile

@@ -12,12 +12,22 @@ ifeq ($(D), )
 	@exit 1
 	@exit 1
 endif
 endif
 
 
-	@$(MAKE) -C boot/ 
-
 # $(PWD) is the / directory of kernel, e.g. /root/evx/
 # $(PWD) is the / directory of kernel, e.g. /root/evx/
 	@$(MAKE) -C $(PWD) D=$(shell pwd) obj-i="$(obj-i)"
 	@$(MAKE) -C $(PWD) D=$(shell pwd) obj-i="$(obj-i)"
 
 
+vmevx.o: $(PWD)/.objlist 
+	@cat $(PWD)/.objlist | xargs $(LD) -o vmevx.o -T vmevx.lds 
+	@echo "LD                vmevx.o"
+
+vmevx.bin: vmevx.o
+	@objcopy -S -R ".comment" -R ".eh_frame" -O binary vmevx.o vmevx.bin 
+
+
+boot: 	vmevx.bin  
+	@$(MAKE) -C boot/ 
+
 
 
 .PHONY clean:
 .PHONY clean:
 	@$(MAKE) -C boot/ clean
 	@$(MAKE) -C boot/ clean
+	@$(MAKE) -C init/ clean
 
 

+ 3 - 2
arch/x86/boot/Makefile

@@ -16,15 +16,16 @@ all: 	Image
 	@echo "LD                $*.S.o"
 	@echo "LD                $*.S.o"
 
 
 %.S.o.bin:	%.S.o
 %.S.o.bin:	%.S.o
-	@objcopy -I elf32-i386 -S -j .text -O binary $*.S.o $*.S.o.bin
+	@objcopy -S -j .text -O binary $*.S.o $*.S.o.bin
 	@rm $*.S.o 
 	@rm $*.S.o 
 	@echo "OBJCOPY           $*.S.o.bin"
 	@echo "OBJCOPY           $*.S.o.bin"
 
 
 
 
-Image:	$(obj-s)
+Image:	$(obj-s) 
 	@dd if=/dev/zero of=Image bs=1048576 count=64 >& /dev/zero 
 	@dd if=/dev/zero of=Image bs=1048576 count=64 >& /dev/zero 
 	@dd if=boot$(BITS).S.o.bin of=Image conv=notrunc >& /dev/zero 
 	@dd if=boot$(BITS).S.o.bin of=Image conv=notrunc >& /dev/zero 
 	@dd if=setup$(BITS).S.o.bin of=Image conv=notrunc bs=512 seek=1 >& /dev/zero 
 	@dd if=setup$(BITS).S.o.bin of=Image conv=notrunc bs=512 seek=1 >& /dev/zero 
+	@dd if=$(PWD)/arch/x86/vmevx.bin of=Image conv=notrunc bs=512 seek=2 >& /dev/zero 
 	@rm *.bin 
 	@rm *.bin 
 	@echo "MKIMG             Image"
 	@echo "MKIMG             Image"
 
 

+ 1 - 1
arch/x86/boot/setup32.S

@@ -114,7 +114,7 @@ init_prot:
 	/* move it to 1mb */
 	/* move it to 1mb */
 
 
 	movl $1048576, %edi
 	movl $1048576, %edi
-	movl $0x8400, %esi
+	movl $0x8200, %esi
 
 
 	movl $131072, %ecx
 	movl $131072, %ecx
 
 

+ 15 - 0
arch/x86/init/Makefile

@@ -0,0 +1,15 @@
+obj-s = head.S.o 
+
+target: $(obj-s)
+	@echo `pwd`/$(obj-s) >> $(PWD)/.objlist 
+
+%.S.o: %.S
+	@$(CPP) $*.S | $(AS) --32 -o $*.S.o 
+	@echo "AS                $*.S.o"
+
+
+.PHONY clean:
+	@rm $(obj-s) -f 
+	@echo "CLEAN 		  OBJS"
+
+

+ 70 - 0
arch/x86/init/head.S

@@ -0,0 +1,70 @@
+/* arch/x86/init/head.S 
+ * 
+ * This file is part of evx. 
+ * 
+ * evx is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * evx is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with evx. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+.code32
+
+
+.globl _start
+
+_start:
+	/* we are now in the protected-mode, just 
+	 * do some initializes, and call main */
+
+	lgdt gdt_descr
+
+	movl $0xc0000000, %eax
+	leal 1f, %ecx
+
+	orl %ecx, %eax
+
+	pushl %eax
+
+	movl $0xc0000000, %eax
+	leal init, %ecx
+
+	orl %ecx, %eax
+
+	pushl %eax
+
+	retl
+
+1:	hlt
+	jmp 1b
+
+
+.data
+
+.align 16
+
+.globl gdt
+
+gdt:	.long 0x00000000, 0x00000000
+	.long 0x0000ffff, 0x00cf9a00
+	.long 0x0000ffff, 0x00cf9200
+
+
+.globl gdt_descr
+
+gdt_descr:
+	.word . - gdt
+	.long gdt
+
+

+ 50 - 0
arch/x86/vmevx.lds

@@ -0,0 +1,50 @@
+OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
+OUTPUT_ARCH(i386)
+ENTRY(_start)
+
+SECTIONS {
+	. = 0xc0000000 + 0x100000;
+
+	.text : {
+		_text = .;
+		*(.text)
+		_etext = .;
+	}
+
+	.init.text : {
+		*(.init.text)
+	}
+
+	.initcalls.arch : {
+		_init_call_arch = .;
+		*(.initcalls.arch)
+		_einit_call_arch = .;
+	}
+
+	.initcalls.kernel : {
+		_init_call_kernel = .;
+		*(.initcalls.kernel)
+		_einit_call_kernel = .;
+	}
+
+	.initcalls.modules : {
+		_init_call_modules = .;
+		*(.initcalls.modules)
+		_einit_call_modules = .;
+	}
+
+	.data : {
+		_data = .;
+		*(.data)
+		_edata = .;
+	}
+
+	.rodata : {
+		_rodata = .;
+		*(.rodata)
+		_erodata = .;
+	}
+
+	_end = .;
+}
+

+ 56 - 0
include/kernel/init.h

@@ -0,0 +1,56 @@
+/* include/kernel/init.h 
+ * 
+ * This file is part of evx. 
+ * 
+ * evx is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * evx is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with evx. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#ifndef __INIT_H
+#define __INIT_H
+
+
+#ifndef __ASSEMBLER__
+
+#include <sys/types.h>
+
+
+#ifndef NULL
+
+#define NULL ((void *) 0)
+
+#endif
+
+/* defines in kernel.lds */
+
+extern void *_init_call_arch, *_init_call_kernel, *_init_call_modules;
+extern void *_einit_call_arch, *_einit_call_kernel, *_einit_call_modules;
+
+#define arch_init(fn) 	\
+	void *__initcall1_##fn __attribute__ ((__section__ (".initcalls.arch"))) = fn;
+
+#define kernel_init(fn)	\
+	void *__initcall2_##fn __attribute__ ((__section__ (".initcalls.kernel"))) = fn;
+
+
+#define __init		__attribute__((__section__(".init.text")))
+
+#define ARRAYSIZE(arr) 	(sizeof(arr) / sizeof(arr[0]))
+
+#endif
+
+#endif
+

+ 76 - 0
include/kernel/module.h

@@ -0,0 +1,76 @@
+/* include/kernel/module.h 
+ * 
+ * This file is part of evx. 
+ * 
+ * evx is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * evx is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with evx. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#ifndef __MODULE_H
+#define __MODULE_H
+
+
+#ifndef __ASSEMBLER__
+
+
+#include <sys/types.h>
+
+struct modfuncs {
+	/* point to blk/chr/net operations */
+	void *ops;
+
+	struct modfuncs *next;
+};
+
+
+struct kmodule {
+	char *mod_name;
+
+	/* this module compiled inside kernel */
+
+#define MODTYPE_KINSIDE 	0x0001
+#define MODTYPE_GPL 		0x0002
+
+#define MODTYPE_BLKDRV 		0x0004
+#define MODTYPE_CHRDRV 		0x0008
+#define MODTYPE_NETDRV 		0x0010
+
+#define MODTYPE_KPATCH 		0x0020
+#define MODTYPE_FS 		0x0040
+
+	/* there is a panic (or oops) caused by this module */
+
+#define MODTYPE_CAUSED_PANIC 	0x0080
+
+	__uint16_t mod_type;
+
+	/* reference count of this module. */
+	__uint32_t mod_refer;
+
+	struct modfuncs *mod_funcs;
+
+	/* the uid of the installer of this module */
+	__uid_t installer_uid;
+};
+
+
+#define module_init(fn) 										\
+	void *__initcall_##fn  __attribute__ ((__section__ (".initcalls.modules"))) = fn;		\
+
+#endif
+
+#endif
+

+ 75 - 0
include/sys/types.h

@@ -0,0 +1,75 @@
+/* include/sys/types.h 
+ * 
+ * This file is part of xnix. 
+ * 
+ * XNIX is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * XNIX is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with XNIX. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#ifndef __TYPES_H
+#define __TYPES_H
+
+
+/* 
+ * this file defines all types of intergers (not include 128-bits), 
+ * in kernel hacking you should use these types, not use raw C types */
+
+/*  
+ * and, you should not use those 64-bit intergers, because xnix will 
+ * run on some 32-bit machines too. 
+ * */
+
+
+#ifndef __ASSEMBLER__
+
+/* signed intergers */
+
+typedef signed char 		__int8_t;
+typedef signed char 		__sint8_t;
+typedef signed char 		__schar_t;
+
+typedef short signed int 	__int16_t;
+typedef short signed int 	__sint16_t;
+
+typedef signed int 		__int32_t;
+typedef signed int 		__sint32_t;
+
+typedef long signed int 	__int64_t;
+typedef long signed int 	__sint64_t;
+
+
+/* and, unsigneds */
+
+typedef unsigned char 		__uint8_t;
+typedef unsigned char 		__uchar_t;
+
+typedef short unsigned int	__uint16_t;
+
+typedef unsigned int 		__uint32_t;
+
+typedef long unsigned int 	__uint64_t;
+
+
+typedef __uint16_t __gid_t;
+typedef __uint32_t __uid_t;
+
+typedef __uint32_t __ref_t;
+typedef __uint32_t __dev_t;
+
+#endif
+
+#endif
+

+ 15 - 0
init/Makefile

@@ -0,0 +1,15 @@
+# this was called from the Makefile in /, 
+# and we just tell it what objects that he should make. 
+
+obj-i = main.c  
+
+target:
+
+ifeq ($(D), )
+	@echo "this should call from the Makefile in directory /, but not typing make in kernel/. :-(" >&2 
+	@exit 1
+endif
+
+# $(PWD) is the / directory of kernel, e.g. /root/evx/
+	@$(MAKE) -C $(PWD) D=$(shell pwd) obj-i="$(obj-i)"
+

+ 33 - 0
init/main.c

@@ -0,0 +1,33 @@
+/* init/main.c 
+ * 
+ * This file is part of evx. 
+ * 
+ * evx is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * evx is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with evx. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#include <kernel/init.h>
+#include <kernel/module.h>
+
+#include <sys/types.h>
+
+
+
+
+int __init init(void) {
+	return 0;
+}
+

+ 3 - 2
scripts/do_build.sh

@@ -3,8 +3,9 @@
 echo "CC                $1"
 echo "CC                $1"
 
 
 # well, that's very, very, very ugly, but at least it works :-(
 # well, that's very, very, very ugly, but at least it works :-(
-# $2: CC, $3: directory to enter, and $4 is the CC flags
+# $2: CC, $3: directory to enter, and $4 is the CC flags, $5 is the 
+# extended CC flags by arch. 
 
 
 cd $3
 cd $3
-$2 $4 -c $1 -o $1.o
+$2 $4 $5 -c $1 -o $1.o