Prechádzať zdrojové kódy

evx: printk(): supporting '%c' and '%s', tomorrow I think I will support '%d' etc. :-)

rain 4 rokov pred
rodič
commit
8a39e99502

+ 1 - 1
Makefile

@@ -23,7 +23,7 @@ KERNEL_VERSION 		= $(KERNEL_MAJOR).$(KERNEL_PATCH_LEVEL).$(KERNEL_SUBVER)\
 			$(KERNEL_COMPILE_TIME) $(KERNEL_COMPILE_BY) on $(ARCH)
 
 MAKEFLAGS		+= --no-print-directory 
-CFLAGS 			+= -I$(PWD)/include/ -g -nostdinc -nostdlib -Wall -Werror 
+CFLAGS 			+= -I$(PWD)/include/ -g -nostdlib -Wall -Werror 
 ARCHCFLAGS 		+= $(shell cat $(ARCHDIR)/.archcflags)
 
 SHELl 			= $(shell echo $$SHELL)

+ 1 - 1
arch/x86/Makefile

@@ -1,7 +1,7 @@
 # this was called from the Makefile in /, 
 # and we just tell it what objects that he should make. 
 
-BITS = 64
+BITS 	:= 64
 
 asm 	= boot/boot$(BITS).S boot/setup$(BITS).S 
 asmobj 	= boot/boot$(BITS).S.o boot/setup$(BITS).S.o 

+ 1 - 3
arch/x86/boot/Makefile

@@ -11,12 +11,10 @@ all: 	Image
 	@$(CPP)	$*.S | $(AS) --32 -o $*.S.o.l 
 	@echo "AS                $*.S.o.l"
 	@$(LD) 	$*.S.o.l -o $*.S.o -T boot.lds 
-	@rm $*.S.o.l 
 	@echo "LD                $*.S.o"
 
 %.S.o.bin:	%.S.o
 	@objcopy -S -j .text -O binary $*.S.o $*.S.o.bin
-	@rm $*.S.o 
 	@echo "OBJCOPY           $*.S.o.bin"
 
 
@@ -25,11 +23,11 @@ Image:	$(obj-s)
 	@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=$(PWD)/arch/x86/vmevx.bin of=Image conv=notrunc bs=512 seek=2 >& /dev/zero 
-	@rm *.bin 
 	@echo "MKIMG             Image"
 
 
 .PHONY clean:
 	@rm Image -f  
+	@rm $*.S.o *.bin *.S.o.l -f 
 	@echo "CLEAN             Image"
 

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

@@ -137,7 +137,7 @@ setup_e820:
 	movl $0x9000, %ebx
 	movl %ebx, %es
 
-	movl $0, %edi
+	xorl %edi, %edi
 	xorl %ebx, %ebx
 
 sel:	movl $0xe820, %eax

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

@@ -1,19 +1,19 @@
 /* arch/x86/boot/setup32.S 
  * 
- * This file is part of xnix. 
+ * This file is part of evx. 
  * 
- * XNIX is free software: you can redistribute it and/or modify 
+ * 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. 
  * 
- * XNIX is distributed in the hope that it will be useful, 
+ * 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 XNIX. If not, see <https://www.gnu.org/licenses/>
+ * along with evx. If not, see <https://www.gnu.org/licenses/>
  */ 
 
 

+ 11 - 13
arch/x86/boot/setup64.S

@@ -68,30 +68,28 @@ real_start:
 	/* finally I used the thing that I used, and this is better, I think.
 	 * initial 2m pages at 0 */
 
+	/* clear them before using */
+
+	xorl %eax, %eax
+	xorl %edi, %edi
+	movl $4096, %ecx
+
+	cld
+ 	rep stosl
+
+
 	movl $0x1007, 0
-	movl $0x0000, 4
 
 	/* 0xffff800000000000 */
 	movl $0x1007, 0x800
-	movl $0, 0x804
-
 	movl $0x2007, 0x1000
-	movl $0, 0x1004
 
 	movl $0x000083, 0x2000
-	movl $0, 0x2004
-
 	movl $0x200083, 0x2008
-	movl $0, 0x200c
-
 	movl $0x400083, 0x2010
-	movl $0, 0x2014
-
 	movl $0x600083, 0x2018
-	movl $0, 0x201c
-
 	movl $0x800083, 0x2020
-	movl $0, 0x2024
+
 
 	ljmp $8, $init_prot
 

+ 1 - 1
arch/x86/include64/arch/archinfo.h

@@ -30,7 +30,7 @@
 typedef __uint64_t __archptr_t;
 
 #define PAGE_SIZE 	2097152lu
-#define PAGE_MASK 	2097152lu
+#define PAGE_MASK 	2097151lu
 #define PAGE_SHIFT 	21lu
 #define PAGE_ALIGN(a) 	(((a) + PAGE_MASK) & ~(PAGE_MASK))
 

+ 6 - 0
arch/x86/init/head32.S

@@ -49,6 +49,12 @@ _start:
 1:	hlt
 	jmp 1b
 
+.globl __stack_chk_fail
+
+__stack_chk_fail:
+	hlt
+	jmp __stack_chk_fail
+
 
 .data
 

+ 7 - 1
arch/x86/init/head64.S

@@ -111,7 +111,13 @@ rsppdp:	stosq
 	movq %rax, %cr3
 
 	ret
-	
+
+
+.globl __stack_chk_fail
+
+__stack_chk_fail:
+	hlt
+	jmp __stack_chk_fail
 
 .data
 

+ 1 - 1
arch/x86/kernel/arch_mm.c

@@ -72,7 +72,7 @@ int arch_mm_init(void) {
 			z[e->type].start_addr = (void *) addr;
 
 
-		z[e->type].total_pages++;
+		z[e->type].total_pages += PAGE_NR(e->len);
 
 
 		/* now initiallize page structures */

+ 53 - 0
include/kernel/kernel.h

@@ -0,0 +1,53 @@
+/* include/kernel/kernel.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 __KERNEL_H
+#define __KERNEL_H
+
+
+#ifndef __ASSEMBLER__
+
+#include <sys/types.h>
+#include <stdarg.h>
+
+/* some useful function declares and macros, as what we did in linux :-) */
+
+
+#ifndef NULL
+#define NULL ((void *) 0)
+#endif
+
+#ifndef ARRAYSIZE
+#define ARRAYSIZE(arr) 	(sizeof(arr) / sizeof(arr[0]))
+#endif
+
+
+int printk(char *, ...);
+int vsprintf(char *buf, const char *fmt, va_list);
+
+#define to_bool(cond) 	!!(cond)
+#define likely(c) 	__builtin_expect(to_bool(c), 1)
+#define unlikely(c) 	__builtin_expect(to_bool(c), 0)
+
+#endif
+
+#endif
+

+ 3 - 0
init/main.c

@@ -21,6 +21,7 @@
 
 #include <kernel/init.h>
 #include <kernel/module.h>
+#include <kernel/kernel.h>
 
 #include <arch/archinfo.h>
 
@@ -34,6 +35,8 @@ extern int cano_putc(int);
 
 int __init init(void) {
 	do_init_calls();
+
+	printk("evx kernel is starting, %a %c %s\n", 100, 'A', "hello");
 	
 	return 0;
 }

+ 1 - 1
kernel/Makefile

@@ -1,7 +1,7 @@
 # this was called from the Makefile in /, 
 # and we just tell it what objects that he should make. 
 
-obj-i = mm.c 
+obj-i = mm.c vsprintf.c 
 
 target:
 

+ 111 - 0
kernel/vsprintf.c

@@ -0,0 +1,111 @@
+/* kernel/vsprintf.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 <kernel/kernel.h>
+
+#include <sys/types.h>
+#include <stdarg.h>
+
+
+int cano_putc(int);
+
+int printk(char *s, ...) {
+	/* 512 bytes are enough for kernel, because kernelers are 
+	 * very very good, I think :-)
+	 *
+	 * but you must know this truth, because more long 's' can 
+	 * make a stack-overflow */
+
+	char buf[512];
+	va_list args;
+	int i;
+
+	va_start(args, s);
+
+	i = vsprintf(buf, s, args);
+
+	va_end(args);
+
+	for (i = 0; buf[i]; i++) 
+		/* will be changed by tty_write etc. in the future */
+		cano_putc(buf[i]);
+
+	return i;
+}
+
+
+/* the function used to format a string, using stdargs */
+#define output(c) 	buf[bufindex++] = c
+
+int vsprintf(char *buf, const char *s, va_list args) {
+	int i, c, state = 0, bufindex;
+
+	char *t;
+       
+	bufindex = 0;
+
+	for (i = 0; (c = s[i]); i++) {
+		if (c == '%') {
+			if (unlikely(!state)) 
+				state = 1;
+
+			else 
+				state = 0;
+
+			continue;
+		}
+
+		else if (likely(!state)) {
+			output(c);
+
+			continue;
+		}
+
+		switch (c) {
+		case 'c':
+			output(va_arg(args, int));
+
+			break;
+
+		case 's':
+			t = va_arg(args, char *);
+
+			while (*t) 
+				output(*t++);
+
+			break;
+
+		default:
+			output('E');
+			(void) va_arg(args, int);
+
+			break;
+		}
+
+		state = 0;
+	}
+
+	return i;
+}
+
+