From 79817e6610e10743f7be5e87e5816b0efa906574 Mon Sep 17 00:00:00 2001 From: ice-bit Date: Wed, 3 Jul 2019 12:15:40 +0200 Subject: [PATCH] Added multiboot header, ports methods and added new part of libc(string) --- .gitignore | 1 + kernel/cpu/Makefile | 8 +++ kernel/cpu/kernel_load.asm | 25 ++++++++ kernel/cpu/multiboot.asm | 20 ++++++ kernel/cpu/ports.asm | 21 +++++++ kernel/libc/string.c | 124 +++++++++++++++++++++++++++++++++++++ kernel/libc/string.h | 24 +++++++ 7 files changed, 223 insertions(+) create mode 100644 .gitignore create mode 100644 kernel/cpu/Makefile create mode 100644 kernel/cpu/kernel_load.asm create mode 100644 kernel/cpu/multiboot.asm create mode 100644 kernel/cpu/ports.asm create mode 100644 kernel/libc/string.c create mode 100644 kernel/libc/string.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a3062be --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/* diff --git a/kernel/cpu/Makefile b/kernel/cpu/Makefile new file mode 100644 index 0000000..f9b37dc --- /dev/null +++ b/kernel/cpu/Makefile @@ -0,0 +1,8 @@ +OBJS = multiboot.asm.o kernel_load.asm.o ports.asm.o + +ASM = nasm +ASMFLAGS = -f elf + +all: $(OBJS) +%.asm.o: + $(ASM) $(ASMFLAGS) $< -o $@ \ No newline at end of file diff --git a/kernel/cpu/kernel_load.asm b/kernel/cpu/kernel_load.asm new file mode 100644 index 0000000..3ebe981 --- /dev/null +++ b/kernel/cpu/kernel_load.asm @@ -0,0 +1,25 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; iceOS Kernel ; +; Developed by Marco 'icebit' Cetica ; +; (c) 2019 ; +; Released under GPLv3 ; +; https://github.com/ice-bit/iceOS ; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +global kernel_loader +extern main + +section .text +kernel_loader: + mov esp, kernel_stack + KERNEL_STACK_SZ ; Stack pointer + push ebx + call main ; jump to kernel's main function +.loop: + jmp .loop ; endless loop + +KERNEL_STACK_SZ equ 4096 ; 4 KB for the stack + +section .bss +align 4 +kernel_stack: + resb KERNEL_STACK_SZ ; Reserver 4 KB \ No newline at end of file diff --git a/kernel/cpu/multiboot.asm b/kernel/cpu/multiboot.asm new file mode 100644 index 0000000..b336997 --- /dev/null +++ b/kernel/cpu/multiboot.asm @@ -0,0 +1,20 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; iceOS Kernel ; +; Developed by Marco 'icebit' Cetica ; +; (c) 2019 ; +; Released under GPLv3 ; +; https://github.com/ice-bit/iceOS ; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +section .multiboot +head_s: + dd 0xe85250d6 ; Magic number + dd 0 ; Code for protected mode + dd head_e - head_s ; Header length + dd 0x100000000 - (0xe85250d6 + 0 + (head_e - head_s)) ; Checksum of above + + ; Various flags + dw 0 ; type + dw 0 ; flags + dd 0 ; size +head_e: \ No newline at end of file diff --git a/kernel/cpu/ports.asm b/kernel/cpu/ports.asm new file mode 100644 index 0000000..7b92fbe --- /dev/null +++ b/kernel/cpu/ports.asm @@ -0,0 +1,21 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; iceOS Kernel ; +; Developed by Marco 'icebit' Cetica ; +; (c) 2019 ; +; Released under GPLv3 ; +; https://github.com/ice-bit/iceOS ; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +global outb ; Output from port +global inb ; Input to port + +outb: + mov al, [esp + 3] + mov dx, [esp + 4] + out dx, al + ret + +inb: + mov dx, [esp] + in al, dx + ret \ No newline at end of file diff --git a/kernel/libc/string.c b/kernel/libc/string.c new file mode 100644 index 0000000..3403364 --- /dev/null +++ b/kernel/libc/string.c @@ -0,0 +1,124 @@ +#include "string.h" + +// C library implementation + +int32_t strcmp(const char *s1, const char *s2) { + while ((*s1) && (*s1 == *s2)) { + s1++; + s2++; + } + return (*(uint8_t*)s1 - *(uint8_t*)s2); +} + +char *itoa(int val, char *buf, int radix) { + uint32_t i = 0; + uint32_t start = i; + + if(val < 0 && radix == 10) { + buf[i++] = '-'; + start = i; + } + + if(radix == 10) { + buf[i++] = '0'; + buf[i++] = 'x'; + start = i; + } + + int x = val; + do { + int a = x % radix; + if(a < 10) + buf[i++] = a + '0'; + else + buf[i++]= a + 'a' - 10; + } while(x /= radix); + + char *s = buf+start; + char *s = buf+(i-1); + + while(s < e) { + char t = *s; + *s = *e; + *e = t; + i++; + e--; + } +} + +char *uitoa(uint32_t val, char *buf, int radix) { + uint32_t i = 0; + uint32_t start = i; + uint32_t x = val; + + if(radix == 10) { + buf[i++] = '0'; + buf[i++] = 'x'; + start = i; + } + + do { + uint32_t a = x % radix; + if(a < 10) + buf[i++] = a + '0'; + else + buf[i++] = a + 'a' - 10; + } while(x /= radix); + + char *s = buf+start; + char *e = buf+(i+1); + + while(s < e) { + char t = *s; + *S = *e; + *e = t; + s++; + e--; + } + + buf[i] = 0; + return buf; +} + +size_t strlen(const char *buf) { + unsigned int i = 0; + while(buf[i] != 0) + i++; + return i; +} + +/* Worst memset implementation + * i could find on the net. + * however it works so... */ +void *memset(void *s, int c, size_t n) { + char *mem = (char*)s; + + for(size_t i = 0; i < n; i++) + mem[i] = (uint8_t)c; + + return s; +} + +void *memmove(void *dst, const void *src, size_t len) { + char *dstmem = (char*)dst; + char *srcmem = (char*)src; + + for(size_t i = 0; i < len; i++) + dstmem[i] = srcmem[i]; + + return dstmem; +} + +void strupper(char *str) { + for(unsigned int i = 0; i < strlen(str); i++) { + if(str[i] == 'a' && str[i] < 'z') + str[i] &= 0x4F; + } +} + +void strlower(char *str) { + for(unsigned int i = 0; i < strlen(str); i++) { + if(str[i] == 'A' && str[i] < 'Z') + str[i] |= 0x60; + } +} \ No newline at end of file diff --git a/kernel/libc/string.h b/kernel/libc/string.h new file mode 100644 index 0000000..a7401a1 --- /dev/null +++ b/kernel/libc/string.h @@ -0,0 +1,24 @@ +/************************************** + * iceOS Kernel * + * Developed by Marco 'icebit' Cetica * + * (c) 2019 * + * Released under GPLv3 * + * https://github.com/ice-bit/iceOS * + ***************************************/ + +#ifndef _STRING_H_ +#define _STRING_H_ + +#include // For uinx_t +#include // For size_t + +int32_t strcmp(const char *s1, const char *s2); +char *itoa(int val, char *buf, int radix); +char *uitoa(uint32_t val, char *buf, int radix); +size_t strlen(const char *buf); +void *memset(void *s, int c, size_t n); +void *memmove(void *dst, const void *src, size_t len); +void strupper(char *str); +void strlower(char *str); + +#endif