瀏覽代碼

evx: wrote a lot, fix them later :-(

rain 4 年之前
父節點
當前提交
27de2f1875

+ 1 - 1
Kconfig

@@ -1,3 +1,3 @@
 [	
-	{}
+	{"CONFIG_64BIT": "Enable / disable 64-bits kernel support"}
 ]

+ 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 -nostdlib -Wall -Werror 
+CFLAGS 			+= -I$(PWD)/include/ -g -nostdinc -nostdlib -Wall -Werror 
 ARCHCFLAGS 		+= $(shell cat $(ARCHDIR)/.archcflags)
 
 SHELl 			= $(shell echo $$SHELL)

+ 1 - 1
arch/x86/.archcflags

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

+ 16 - 3
arch/x86/include/arch/mm.h

@@ -26,7 +26,7 @@
 #ifndef __ASSEMBLER__
 
 #include <arch/archinfo.h>
-
+#include <config.h>
 
 
 #define E820_ADDR 	((struct e820_entry *) 0x90000)
@@ -38,12 +38,25 @@
 
 
 struct e820_entry {
-	__archptr_t 	addr;
-	__archptr_t 	len;
+	__uint32_t 	addrl, addrh;
+	__uint32_t 	lenl, lenh;
+
 	__uint32_t 	type;
 } __attribute__ ((packed));
 
+#if CONFIG_64BIT
+#define HIGH_AND_LOW(high, low, bits) 	((((u64) high) << bits) + (u64) low)
+#else
+#define HIGH_AND_LOW(high, low, bits) 	(low)
+#endif
+
 
+#if CONFIG_64BIT
+#define invalidate() 			__asm__ volatile ("movq %%cr3, %%rax;movq %%rax, %%cr3;" ::: "ax")
+#else
+#define invalidate() 			__asm__ volatile ("movl %%cr3, %%eax;movl %%eax, %%cr3;" ::: "ax")
 #endif
 
 #endif
+#endif
+

+ 32 - 0
arch/x86/include/arch/types.h

@@ -0,0 +1,32 @@
+/* arch/x86/include/arch/string.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_STRING_H
+#define __ARCH_STRING_H
+
+
+#ifndef __ASSEMBLER__
+
+typedef u16 		__io_t;
+
+#endif
+
+#endif

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

@@ -49,8 +49,9 @@ _start:
 1:	hlt
 	jmp 1b
 
-.globl __stack_chk_fail
+.globl __stack_chk_fail, __stack_chk_fail_local
 
+__stack_chk_fail_local:
 __stack_chk_fail:
 	hlt
 	jmp __stack_chk_fail

+ 8 - 18
arch/x86/init/head64.S

@@ -83,29 +83,19 @@ setup_stack:
 
 
 setup_paging:
-	/* setup pde, and reload segment registers */
+	/* map 0-1g memory (512 entries), 
+	 * use memory from 0x2000 ~ 0x3000 */
 
-	movq $3, %rax
-	movq $0x3000, %rdi
+	movq $0x2000, %rdi
+	movq $0x83, %rax
 
 	cld
 
-rspde: 	stosq
-	addq $4096, %rax
-
-	cmpq $0x80000, %rdi
-	jbe rspde
-
-	/* now pdpte */
-	
-	movq $0x3003, %rax
-	movq $0x2000, %rdi
-
-rsppdp:	stosq
-	addq $4096, %rax
-	cmpq $0x80003, %rax
+spl: 	stosq
+	addq $2097152, %rax
 
-	jbe rsppdp
+	cmpq $0x3000, %rdi
+	jbe spl
 
 	xorq %rax, %rax
 	movq %rax, %cr3

+ 89 - 11
arch/x86/kernel/arch_mm.c

@@ -31,7 +31,7 @@
 #include <sys/types.h>
 
 
-extern char cano_putc(int);
+static int __init init_mapping_16m(void);
 
 
 /* the most important work is: get total size of ram, 
@@ -46,38 +46,40 @@ int arch_mm_init(void) {
 
 	__archptr_t		addr;
 
+	init_mapping_16m();
 
 	/* put them there, after that, are the page structures. */
 	z = (struct zone *) &_end;
-	p = (struct page *) ((struct zone *) &_end + 5);
+	p = (struct page *) ((struct zone *) &_end + 2);
 
 	
-	memset(z, 0, sizeof(struct zone) * 5);
+	memset(z, 0, sizeof(struct zone) * 2);
 
 	z[0].type = ZONETYPE_RAM;
-
-
-	for (i = 1; i < 5; i++) 
-		z[i].type = ZONETYPE_OTHER;
+	z[1].type = ZONETYPE_OTHER;
 
 
 	for (e = E820_ADDR; e < E820_ADDR + 32; e++) {
+		long npages;
+
 		if (e->type > E820_TYPE_MAX || !e->type) 
 			break;
+		
+		e->type = e->type == E820_TYPE_RAM ? ZONETYPE_RAM : ZONETYPE_OTHER;
 
-		addr = PAGE_ALIGN(e->addr);
+		addr = PAGE_ALIGN(HIGH_AND_LOW(e->addrh, e->addrl, 32));
 
 
 		if (z[e->type].start_addr > (void *) addr) 
 			z[e->type].start_addr = (void *) addr;
 
 
-		z[e->type].total_pages += PAGE_NR(e->len);
-
+		npages = PAGE_NR(HIGH_AND_LOW(e->lenh, e->lenl, 32));
+		z[e->type].total_pages += npages;
 
 		/* now initiallize page structures */
 
-		for (i = 0; i < PAGE_NR(e->len); i++) {
+		for (i = 0; i < npages; i++) {
 			p->pnr = PAGE_NR(addr);
 			p->count = 0;
 			p->next = p + 1;
@@ -91,6 +93,82 @@ int arch_mm_init(void) {
 
 
 
+/* for a 4gb memory space, 12m used to create struct page */
+
+static int __init init_mapping_16m(void) {
+#if CONFIG_64BIT
+	/* already mapped to 1g */
+	return 0;
+#else
+
+	__archptr_t *lvl1 = (void *) 0, 
+		    *lvl2 = (void *) 4096;
+
+	__archptr_t addr = 7;
+	int i, j;
+
+	lvl1[0] = 4096 + 7;
+	lvl1[1] = 8192 + 7;
+	lvl1[2] = 4096 * 3 + 7;
+	lvl1[3] = 4096 * 4 + 7;
+
+	lvl1[0xc00] = 4096 + 7;
+	lvl1[0xc01] = 8192 + 7;
+	lvl1[0xc02] = 4096 * 3 + 7;
+	lvl1[0xc03] = 4096 * 4 + 7;
+
+	
+	for (i = 0; i < 4; i++) {
+		lvl2 = (void *) (lvl1[i] & ~0xff);
+
+		for (j = 0; j < 1024; j++, addr += 4096) 
+			lvl2[j] = addr;
+	}
+
+	invalidate();
+
+	return 0;
+#endif
+}
+
+
+/* pmap */
+#define START 	0x3000
+
+/* stack - 8192 */
+#define END 	0x8e000
+
+
+__archptr_t 	freeptr = START;
+
+
+void *early_kmalloc(int nbytes, int align) {
+	void *p;
+
+	freeptr = (freeptr + align - 1) & ~(align - 1);
+
+	if (freeptr + nbytes > END) 
+		return NULL;
+
+	p = (void *) freeptr;
+	freeptr += nbytes;
+
+	return p;
+}
+
+
+int early_free(void *p, int nbytes) {
+	if (p + nbytes != (void *) freeptr) 
+		/* cannot free */
+		return 1;
+
+	freeptr -= nbytes;
+
+	return 0;
+}
+
+
+
 arch_init(arch_mm_init);
 
 

+ 6 - 0
include/kernel/kernel.h

@@ -47,6 +47,12 @@ int vsprintf(char *buf, const char *fmt, va_list);
 #define likely(c) 	__builtin_expect(to_bool(c), 1)
 #define unlikely(c) 	__builtin_expect(to_bool(c), 0)
 
+#define isdigit(c) 	((c) >= '0' && (c) <= '9')
+#define isnrx(c) 	(isdigit(c) || ((c) >= 'a' && (c) <= 'f'))
+#define isnrX(c) 	(isdigit(c) || ((c) >= 'A' && (c) <= 'F'))
+#define atoi(c) 	((c) - '0')
+
+
 #endif
 
 #endif

+ 4 - 1
include/kernel/mm.h

@@ -37,7 +37,7 @@
  * inserting. 
  *
  *
- * we should initiallize this as early as we can. 
+ * we should initiallize this as late as we can, after maps okay. 
  * */
 
 struct page {
@@ -66,6 +66,9 @@ struct zone {
 	struct page 	*pages;
 };
 
+#define kmem_zones 	((struct zone *) &_end)
+#define kmem_pages 	
+
 
 #endif
 

+ 51 - 0
include/kernel/tty.h

@@ -0,0 +1,51 @@
+/* include/kernel/tty.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 __TTY_H
+#define __TTY_H
+
+
+#ifndef __ASSEMBLER__
+
+#include <sys/types.h>
+
+struct tty {
+	char 		*dev_name;
+	int 		priv_data;
+
+#define FLAGS_READABLE 	1
+#define FLAGS_COLORS 	2
+
+	int 		flags;
+	
+	int 		(* putc) (char);
+	struct tty 	*next;
+
+	/* optional */
+	char 		(* getc) (void);
+};
+
+int tty_register(struct tty *);
+
+
+#endif
+#endif
+

+ 59 - 0
include/lib/klist.h

@@ -0,0 +1,59 @@
+/* evx/include/lib/klist.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 __KLIST_H
+#define __KLIST_H
+
+#include <sys/types.h>
+
+
+struct klist_node {
+	long 		value;
+	int 		xmask;
+	void 		*payload;
+	void 		*data;
+
+	struct klist_node *next, *prev;
+};
+
+
+
+struct klist_node *klist_find_value(struct klist_node *head, int value);
+
+void klist_init(struct klist_node *head);
+
+void klist_add(struct klist_node *head, struct klist_node *new);
+void klist_del(struct klist_node *head, struct klist_node *will_del);
+
+#define klist_empty(list) ((list)->next == (list))
+
+#define KLIST_NODE_INIT(__x) { \
+	.value = 0, \
+	.xmask = 0, \
+	.payload = NULL, \
+	.data = NULL, \
+	.next = __x, \
+	.prev = __x, \
+}
+
+
+#endif
+

+ 36 - 0
include/modules/serial.h

@@ -0,0 +1,36 @@
+/* include/modules/console.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 __MOD_CONSOLE_H
+#define __MOD_CONSOLE_H
+
+
+#ifndef __ASSEMBLER__
+
+#include <sys/types.h>
+
+#define COM0 	0x3f8
+
+
+#endif
+
+#endif
+

+ 39 - 0
include/stdarg.h

@@ -0,0 +1,39 @@
+/* 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 __STDARG_H
+#define __STDARG_H
+
+
+#ifndef __ASSEMBLER__
+
+#include <sys/types.h>
+
+typedef __builtin_va_list	va_list;
+
+#define va_start(v, l)	 	__builtin_va_start(v, l)
+#define va_arg(v, l) 		__builtin_va_arg(v, l)
+#define va_end(v) 		__builtin_va_end(v);
+
+#endif
+
+#endif
+

+ 16 - 6
include/sys/types.h

@@ -29,6 +29,7 @@
 
 /*  
  * and, you should not use those 64-bit intergers, because xnix will 
+ * (okay, I mean, evx)
  * run on some 32-bit machines too. 
  * */
 
@@ -57,17 +58,26 @@ 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 __uint16_t 		__gid_t;
+typedef __uint32_t 		__uid_t;
+
+typedef __uint32_t 		__ref_t;
+typedef __uint32_t 		__dev_t;
+
+typedef __int8_t 		s8;
+typedef __int16_t 		s16;
+typedef __int32_t 		s32;
+typedef __int64_t 		s64;
+
+typedef __uint8_t 		u8;
+typedef __uint16_t 		u16;
+typedef __uint32_t 		u32;
+typedef __uint64_t 		u64;
 
-typedef __uint32_t __ref_t;
-typedef __uint32_t __dev_t;
 
 #endif
 

+ 2 - 1
init/main.c

@@ -22,6 +22,7 @@
 #include <kernel/init.h>
 #include <kernel/module.h>
 #include <kernel/kernel.h>
+#include <kernel/mm.h>
 
 #include <arch/archinfo.h>
 
@@ -36,7 +37,7 @@ extern int cano_putc(int);
 int __init init(void) {
 	do_init_calls();
 
-	printk("evx kernel is starting, %a %c %s\n", 100, 'A', "hello");
+	printk("total ram: %d kib\n", kmem_zones[ZONETYPE_RAM].total_pages << PAGE_SHIFT >> 10);
 	
 	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 
+obj-i = mm.c vsprintf.c tty.c 
 
 target:
 

+ 45 - 0
kernel/tty.c

@@ -0,0 +1,45 @@
+/* kernel/tty.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/tty.h>
+
+#include <sys/types.h>
+#include <lib/klist.h>
+
+
+int tty_register(struct tty *tty) {
+	return /* until the mem isn't okay */ -1;
+}
+
+
+int tty_write(struct tty *tty, char *s) {
+	int err;
+
+	while (*s++) 
+		if ((err = tty->putc(*s) < 0)) 
+			return err;
+
+	return 0;
+}
+
+

+ 79 - 17
kernel/vsprintf.c

@@ -27,6 +27,12 @@
 #include <stdarg.h>
 
 
+/* vsprintf() are almost copied from linux, but ... well, I have no 
+ * words to say. 
+ *
+ * and you should know, we removed a lot of useless things (useful in 
+ * userspace) in kernel. */
+
 int cano_putc(int);
 
 int printk(char *s, ...) {
@@ -54,34 +60,77 @@ int printk(char *s, ...) {
 }
 
 
+static const char digits[] = "0123456789abcdef";
+
+#define ZERO		1
+#define UNSIGNED 	2
+
+
+static int number(char *buf, long nr, int base, int flags, int width) {
+	int neg = 0, i = 0, j;
+	char t[16] = { 0 };
+
+	if (nr < 0) {
+		nr = -nr;
+
+		if (!(flags & UNSIGNED)) 
+			neg = 1;
+	}
+
+	do {
+		t[i++] = digits[nr % base];
+	} while (nr /= base);
+
+
+	if (neg) 
+		t[i++] = '-';
+	
+	if (flags & ZERO) {
+		if (width < 0) 
+			width = 16;
+
+		for (i = 0; i <= width; i++) 
+			if (!t[i]) 
+				t[i] = 0;
+	}
+
+	j = i;
+
+	while (--j >= 0) 
+		buf[i - j - 1] = t[j];
+
+	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;
+	int i, bufindex, flag;
 
-	char *t;
+	char *t, *fmt;
        
 	bufindex = 0;
 
-	for (i = 0; (c = s[i]); i++) {
-		if (c == '%') {
-			if (unlikely(!state)) 
-				state = 1;
-
-			else 
-				state = 0;
+	for (i = 0, fmt = (char *) s; *fmt; fmt++, i++) {
+		if (likely(*fmt != '%')) {
+			output(*fmt);
 
 			continue;
 		}
 
-		else if (likely(!state)) {
-			output(c);
+		/* skip '%' */
+		fmt++;
 
-			continue;
+		if (*fmt == '0') {
+			flag |= ZERO;
+
+			fmt++;
 		}
 
-		switch (c) {
+		switch (*fmt) {
 		case 'c':
 			output(va_arg(args, int));
 
@@ -89,20 +138,33 @@ int vsprintf(char *buf, const char *s, va_list args) {
 
 		case 's':
 			t = va_arg(args, char *);
-
+			
 			while (*t) 
 				output(*t++);
 
 			break;
 
+		case 'd':
+			bufindex += number(buf + bufindex, va_arg(args, int), 10, flag, -1);
+
+			break;
+
+		case 'x':
+		case 'p':
+			/* ignore upcase */
+
+			bufindex += number(buf + bufindex, va_arg(args, int), 16, flag | UNSIGNED, 16);
+
+			break;
+
 		default:
-			output('E');
+			output('%');
+			output(*fmt);
+
 			(void) va_arg(args, int);
 
 			break;
 		}
-
-		state = 0;
 	}
 
 	return i;

+ 70 - 0
lib/klist.c

@@ -0,0 +1,70 @@
+/* evx/klist.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 <sys/types.h>
+#include <lib/klist.h>
+
+
+
+struct klist_node *klist_find_value(struct klist_node *head, int value) {
+	struct klist_node *t;
+
+	for (t = head; t; t = t->next) 
+		if (t->value == value) 
+			return t;
+
+	return NULL;
+}
+
+
+void klist_init(struct klist_node *head) {
+	head->xmask = 0;
+	head->next = head;
+	head->prev = head;
+	head->value = 0;
+	head->payload = NULL;
+	head->data = NULL;
+
+	return;
+}
+
+
+void klist_add(struct klist_node *head, struct klist_node *new) {
+	/* head->prev: the last. */
+	new->prev = head->prev;
+	new->next = head;
+
+	/* head->prev->next: (was) the head */
+	head->prev->next = new;
+	head->prev = new;
+
+	return;
+}
+
+
+void klist_del(struct klist_node *head, struct klist_node *will_del) {
+	will_del->prev->next = will_del->next;
+	will_del->next->prev = will_del->prev;
+
+	return;
+}
+
+

+ 1 - 1
modules/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 = console.c 
+obj-i = console.c serial.c 
 
 target:
 

+ 83 - 0
modules/serial.c

@@ -0,0 +1,83 @@
+/* modules/console.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 <sys/types.h>
+
+#include <kernel/init.h>
+#include <kernel/module.h>
+#include <kernel/tty.h>
+
+#include <modules/serial.h>
+
+#include <arch/io.h>
+#include <arch/types.h>
+
+
+int serial_putc(char);
+
+struct tty serial0_tty = {
+	.dev_name = "ttyS0", 
+	.priv_data = COM0, 
+	.flags = 0, 
+	.putc = serial_putc, 
+	.getc = NULL
+};
+
+
+static int com_init(__io_t p) {
+	outb(0x80, p + 3);
+	outb(0x30, p + 0);
+	outb(0x00, p + 1);
+
+	outb(0x03, p + 3);
+	outb(0x0b, p + 4);
+	outb(0x0d, p + 1);
+
+	inb(p);
+
+	return 0;
+}
+
+/* as i know, this only runs on x86 */
+
+int __init init_serial(void) {
+	com_init(COM0);
+
+	return tty_register(&serial0_tty);
+}
+
+int serial_write(__io_t p, char c) {
+	while (!(inb(p + 5) & 0x20));
+
+	outb(c, p);
+
+	return 0;
+}
+
+
+int serial_putc(char c) {
+	return serial_write(COM0, c);
+}
+
+
+module_init(init_serial);
+
+