Procházet zdrojové kódy

evx: not working, strange bug but i must go over lessons because there's going to be a test

rain před 4 roky
rodič
revize
e268d4454f

+ 1 - 1
Makefile

@@ -36,7 +36,7 @@ ifeq 	($(CROSS_COMPILE), )
 endif
 
 ifeq 	($(CROSS_LD), )
-	CROSS_LD = $(LD)
+	CROSS_LD 	= $(LD)
 endif
 
 

+ 1 - 1
arch/x86/.archcflags

@@ -1 +1 @@
--m32 -I/root/evx/arch/x86/include -I/root/evx/arch/x86/include32 
+-m64 -I/root/evx/arch/x86/include -I/root/evx/arch/x86/include64 

+ 101 - 0
arch/x86/include/arch/arch_task.h

@@ -0,0 +1,101 @@
+/* arch/x86/include/arch/arch_task.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 __ARCH_TASK_H
+#define __ARCH_TASK_H
+
+#include <kernel/task.h>
+#include <arch/archinfo.h>
+#include <arch/int.h>
+#include <arch/string.h>
+
+#ifndef __ASSEMBLER__
+
+extern void ret_intr(void);
+extern void die(void);
+
+/* it's crazy */
+#define STACK_SIZE 		512
+
+struct arch_task_descr {
+	/* current stack pointer */
+	__archptr_t 		sp;
+	/* page table base */
+	__archptr_t 		ptable;
+	/* segment selector, data */
+	__archptr_t 		sel;
+};
+
+#define ARCH_TASK_DESCR_INIT { \
+	.sp = 0, \
+	.ptable = 0, \
+	.sel = 0x08 \
+}
+
+static inline struct arch_task_descr *arch_task_descr_init(struct arch_task_descr *p) {
+	p->sp = 0;
+	p->ptable = 0;
+	p->sel = 0x08;
+
+	return p;
+}
+
+
+static inline int arch_stack_init(struct arch_task_descr *p) {
+	struct registers *regs;
+
+	if (unlikely(!(p->sp = (__archptr_t) early_kmalloc(STACK_SIZE, 1)))) 
+		panic("arch_stack_init: Out of memory");
+
+	p->sp += STACK_SIZE;
+
+	regs = (struct registers *) ((void *) p->sp - sizeof(struct registers));
+	memset(regs, 0, sizeof(struct registers));
+
+	regs->gs = regs->fs = regs->es = regs->ds = 0x10;
+	regs->ip = (__archptr_t) die;
+	regs->cs = 0x08;
+	regs->flags = 0x20;
+	regs->sp = p->sp;
+	regs->ss = 0x10;
+
+	return 0;
+}
+
+/* it's not so easy to understand this. we just pop the registers in the stack 
+ * and return. for the first time, the ip (in stack but not in a structure) is 
+ * ret_intr */
+
+#define switch_to(task) ({ 				\
+	__asm__ volatile ( 				\
+		"movq %%rsp, %0\n\t" 			\
+		"movq %1, %%rsp\n\t" 			\
+		"jmp ret_intr\n\t" 			\
+		: "=m" (current->arch_descr.sp) 	\
+		: "m" (task->arch_descr.sp) 		\
+	);						\
+})
+
+
+#endif
+#endif
+
+

+ 8 - 0
arch/x86/include/arch/int.h

@@ -55,6 +55,14 @@ struct registers {
 	__archptr_t 	errcode, ip, cs, flags, sp, ss;
 };
 
+struct ret_intr_regs {
+	__archptr_t 	errcode, ip, cs, flags, sp, ss;
+	__archptr_t 	di, si, bp, dx, bx, cx, ax;
+#if CONFIG_64BIT
+	__archptr_t 	r15, r14, r13, r12, r11, r10, r9, r8;
+#endif
+};
+
 #endif
 #endif
 

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

@@ -113,9 +113,11 @@ ret_intr:
 
 
 .globl __stack_chk_fail, __stack_chk_fail_local
+.globl die
 
 __stack_chk_fail_local:
 __stack_chk_fail:
+die:
 	hlt
 	jmp __stack_chk_fail
 

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

@@ -33,7 +33,6 @@ _start:
 	call init_idt
 	call setup_paging
 
-
 	movq $0xffff800000000000, %rax
 	leaq 1f(%rip), %rcx
 
@@ -178,8 +177,10 @@ ret_intr:
 
 
 .globl __stack_chk_fail
+.globl die
 
 __stack_chk_fail:
+die:
 	hlt
 	jmp __stack_chk_fail
 

+ 0 - 1
include/kernel/init.h

@@ -62,6 +62,5 @@ extern int init_state;
 #define ARRAYSIZE(arr) 	(sizeof(arr) / sizeof(arr[0]))
 
 #endif
-
 #endif
 

+ 63 - 0
include/kernel/task.h

@@ -0,0 +1,63 @@
+/* include/kernel/task.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 __TASK_H
+#define __TASK_H
+
+
+#ifndef __ASSEMBLER__
+
+#include <sys/types.h>
+#include <arch/arch_task.h>
+
+
+/* exit()'ed, but not be wait()'ed by its parent */
+#define TASK_ZOMB 		0
+/* it's ready to run but not running */
+#define TASK_READY 		1
+/* running on current cpu, and the current points to the struct task of it */
+#define TASK_RUNNING 		2
+/* waiting for some resources */
+#define TASK_WAIT 		3
+
+struct task {
+	int 			state;
+	int 			pid;
+
+	struct task 		*parent;
+
+	/* other things such as pgid or sid are strange for me, well, 
+	 * support them later */
+
+	/* for x86, it includes sp, ptable etc */
+	struct arch_task_descr 	arch_descr;
+	int 			nice;
+};
+
+extern struct task 		*current;
+extern struct task 		init_task;
+
+/* returns a new task copied from current */
+struct task *dup_task(void);
+
+#endif
+#endif
+

+ 0 - 7
init/main.c

@@ -38,18 +38,11 @@ static int do_init_calls(void);
 
 
 int __init init(void) {
-	struct page *p = NULL;
-	int i;
-
 	init_state = STATE_INIT_CALLS;
 
 	do_init_calls();
-
 	init_state = STATE_READY;
 
-	i = 1 / (long) p;
-	kmlog("%d\n", i);
-	
 	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 vsprintf.c tty.c mempool.c panic.c page.c kmlog.c 
+obj-i = mm.c vsprintf.c tty.c mempool.c panic.c page.c kmlog.c task.c 
 
 target: $(obj-i)
 

+ 78 - 0
kernel/task.c

@@ -0,0 +1,78 @@
+/* kernel/task.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/kernel.h>
+#include <kernel/module.h>
+#include <kernel/mm.h>
+#include <kernel/task.h>
+#include <kernel/kmlog.h>
+
+#include <arch/archinfo.h>
+
+#include <sys/types.h>
+
+
+struct task init_task = {
+	.state = TASK_RUNNING, 
+	.pid = 0, 
+	.parent = NULL, 
+	.arch_descr = ARCH_TASK_DESCR_INIT, 
+	.nice = 0
+};
+
+struct task *current = &init_task;
+int next_pid = 1;
+
+
+int task_init(void) {
+	struct task *new = dup_task();
+
+	kmlog("New task: %p\n", new);
+	kmlog("task initialization finished. \n");
+
+	switch_to(new);
+
+	return 0;
+}
+
+
+struct task *dup_task(void) {
+	struct task *tsk;
+
+	if (unlikely(!(tsk = kmalloc(sizeof(struct task))))) 
+		panic("dup_task: Out of memory");
+
+	tsk->state = TASK_WAIT;
+	tsk->pid = next_pid++;
+	tsk->parent = current;
+	tsk->nice = 0;
+
+	arch_task_descr_init(&tsk->arch_descr);
+	arch_stack_init(&tsk->arch_descr);
+	tsk->state = TASK_READY;
+
+	return tsk;
+}
+
+
+kernel_init(task_init);
+