aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2021-04-21 12:23:54 +0200
committerLoic Guegan <manzerbredes@mailbox.org>2021-04-21 12:23:54 +0200
commitd9443c7fdf756212bb52ffc934b1166038bc2ad3 (patch)
treecf31082457e159a7de9a5bb04f15edc598afddf5 /src
parentca1e725b0dc9b10997897dd2ac6d44028601d9bb (diff)
Refactoring
Diffstat (limited to 'src')
-rw-r--r--src/Makefile18
-rw-r--r--src/boot/boot.S29
-rw-r--r--src/boucane.c9
-rw-r--r--src/boucane.cc11
-rw-r--r--src/core/types.hpp (renamed from src/core/types.h)5
-rw-r--r--src/drivers/framebuffer.cc (renamed from src/drivers/framebuffer.c)2
-rw-r--r--src/drivers/framebuffer.hpp (renamed from src/drivers/framebuffer.h)6
-rw-r--r--src/include/boucane.hpp11
-rw-r--r--src/libc/math.c32
-rw-r--r--src/libc/math.h9
-rw-r--r--src/libc/stdio.c25
-rw-r--r--src/libc/stdio.h21
-rw-r--r--src/libc/string.c29
-rw-r--r--src/libc/string.h9
-rw-r--r--src/libs/math.cc32
-rw-r--r--src/libs/math.hpp6
-rw-r--r--src/libs/stdio.cc123
-rw-r--r--src/libs/stdio.hpp36
-rw-r--r--src/libs/string.cc71
-rw-r--r--src/libs/string.hpp23
-rw-r--r--src/linker.ld3
21 files changed, 353 insertions, 157 deletions
diff --git a/src/Makefile b/src/Makefile
index 7c2ee5f..1c2b408 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,25 +1,25 @@
EXEC := boucane
-CC := gcc -c -fno-pie -fno-builtin -fno-stack-protector -I ./
+CC := g++ -nostdlib -nostdinc -no-pie -fno-builtin -fno-stack-protector -I ./ -I include
LD_SCRIPT := linker.ld
# Note that BOOT_OBJ do not match boot.S
# Indeed boot.o generated by boot.S should appear
# first in the kernel binary (thus it must be linked first, cf the $(EXEC) rule)
-BOOT_OBJ := $(addsuffix .o,$(basename $(shell find ./boot -name "*.[c|S]" ! -name "boot.S")))
-DRIVERS_OBJ := $(addsuffix .o,$(basename $(shell find ./drivers -name "*.[c|S]")))
-LIBC_OBJ := $(addsuffix .o,$(basename $(shell find ./libc -name "*.[c|S]")))
+BOOT_OBJ := $(addsuffix .cc,$(basename $(shell find ./boot -name '*.cc' -name '*.S' ! -name "boot.S")))
+DRIVERS_OBJ := $(addsuffix .o,$(basename $(shell find ./drivers -name '*.cc' -o -name '*.S')))
+LIBS_OBJ := $(addsuffix .cc,$(basename $(shell find ./libs -name '*.cc' -o -name '*.S')))
all: $(EXEC)
-$(EXEC): boot/boot.o $(BOOT_OBJ) $(DRIVERS_OBJ) $(LIBC_OBJ) boucane.o
- ld -n -T $(LD_SCRIPT) -nostdlib -o $@ $^
+$(EXEC): boot/boot.o $(BOOT_OBJ) $(DRIVERS_OBJ) $(LIBS_OBJ) boucane.o
+ $(CC) -n -T $(LD_SCRIPT) -nostdlib -o $@ $^
%.o: %.S
as -o $@ $^
-%.o: %.c
- $(CC) -o $@ $<
-
+%.o: %.cc
+ $(CC) -c -o $@ $^
+
clean:
rm -f $(EXEC)
find ./ -name "*.o" -delete
diff --git a/src/boot/boot.S b/src/boot/boot.S
index 676ace7..8391e70 100644
--- a/src/boot/boot.S
+++ b/src/boot/boot.S
@@ -1,6 +1,5 @@
.globl _start
.globl MB_INFO
-.extern boucane
.extern _bss_start
.extern _bss_end
.extern boucane
@@ -49,7 +48,16 @@ MB_INFO:
_start:
mov %ebx,(MB_INFO)
-
+# Zeroing the .bss section
+mov $_bss_start, %eax
+mov $_bss_end, %ebx
+start_zeroing:
+ movb $0x0, (%eax)
+ cmp %eax, %ebx
+ je end_zeroing
+ inc %eax
+ jmp start_zeroing
+end_zeroing:
# ----- Setup PAE Paging (identity on the first 10MB)
mov $8192, %ecx # 8*4096/4 (8 tables of 4096 byte each divide by 4 because of movl)
mov $0, %eax
@@ -104,7 +112,20 @@ lgdt (gdtr)
jmp $0x08, $new_cs
new_cs:
+mov $0x10, %ax
+mov %ax, %ds
+mov %ax, %es
+mov %ax, %fs
+mov %ax, %gs
+mov %ax, %ss
+
+# Pay attention here!
+# You are in long mode here
+# Thus if you want to add any instructions
+# You should use .code64 before
+
# Run boucane
+.code64
jmp boucane
# GDT
@@ -121,8 +142,8 @@ gdt64:
gdt64_ds:
.long 0
.byte 0
- .byte 0b10000000
+ .byte 0b10010010
.word 0
gdtr:
.word . - gdt64 - 1
- .quad gdt64 \ No newline at end of file
+ .quad gdt64
diff --git a/src/boucane.c b/src/boucane.c
deleted file mode 100644
index 1b80891..0000000
--- a/src/boucane.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include "libc/stdio.h"
-
-
-
-void boucane(){
- clear();
- print("Boucane is booting...");
- while(1);
-} \ No newline at end of file
diff --git a/src/boucane.cc b/src/boucane.cc
new file mode 100644
index 0000000..74dad9d
--- /dev/null
+++ b/src/boucane.cc
@@ -0,0 +1,11 @@
+#include "boucane.hpp"
+
+extern "C" void boucane(){
+ clear();
+ char a[]="Loic Guegan";
+ printk("Booting Boucane v%d.%d.%d",VERSION_MAJOR,VERSION_MINOR, VERSION_PATH);
+
+
+
+ while(1);
+} \ No newline at end of file
diff --git a/src/core/types.h b/src/core/types.hpp
index 071d8d6..61baf3f 100644
--- a/src/core/types.h
+++ b/src/core/types.hpp
@@ -1,9 +1,6 @@
-#ifndef TYPES_H
-#define TYPES_H
+#pragma once
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
-
-#endif
diff --git a/src/drivers/framebuffer.c b/src/drivers/framebuffer.cc
index 110dc73..b945e77 100644
--- a/src/drivers/framebuffer.c
+++ b/src/drivers/framebuffer.cc
@@ -1,4 +1,4 @@
-#include "framebuffer.h"
+#include "framebuffer.hpp"
#define MAX_COL 80
#define MAX_LINE 25
diff --git a/src/drivers/framebuffer.h b/src/drivers/framebuffer.hpp
index d265015..fb09d6f 100644
--- a/src/drivers/framebuffer.h
+++ b/src/drivers/framebuffer.hpp
@@ -1,7 +1,6 @@
-#ifndef FRAMEBUFFER_H
-#define FRAMEBUFFER_H
+#pragma once
-#include "core/types.h"
+#include "core/types.hpp"
typedef enum VIDEO_COLORS {
BLACK=0, BLUE=1, GREEN=2,CYAN=3, RED=4,PURPLE=5,BROWN=6,GRAY=7,
@@ -32,4 +31,3 @@ void scrollup();
*/
void clear();
-#endif
diff --git a/src/include/boucane.hpp b/src/include/boucane.hpp
new file mode 100644
index 0000000..d78db60
--- /dev/null
+++ b/src/include/boucane.hpp
@@ -0,0 +1,11 @@
+#pragma once
+
+#define VERSION_MAJOR 0
+#define VERSION_MINOR 1
+#define VERSION_PATH 0
+
+#include "core/types.hpp"
+#include "libs/math.hpp"
+#include "libs/stdio.hpp"
+#include "libs/string.hpp"
+
diff --git a/src/libc/math.c b/src/libc/math.c
deleted file mode 100644
index 80b1d3a..0000000
--- a/src/libc/math.c
+++ /dev/null
@@ -1,32 +0,0 @@
-#include "math.h"
-
-int pow(int x,int n){
- if(n<0)
- return -1;
- else if(n==0)
- return 1;
- else if(n==1)
- return x;
- int ret=x;
- for(int i=0;i<(n-1);i++)
- ret*=x;
- return ret;
-}
-
-int max(int x,int y){
- if(x>y)
- return x;
- return y;
-}
-
-int min(int x,int y){
- if(x<y)
- return x;
- return y;
-}
-
-int abs(int x){
- if(x<0)
- return -x;
- return x;
-} \ No newline at end of file
diff --git a/src/libc/math.h b/src/libc/math.h
deleted file mode 100644
index 4cc37a9..0000000
--- a/src/libc/math.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#ifndef MATH_H
-#define MATH_H
-
-int pow(int x,int n);
-int max(int x,int y);
-int min(int x,int y);
-int abs(int x);
-
-#endif \ No newline at end of file
diff --git a/src/libc/stdio.c b/src/libc/stdio.c
deleted file mode 100644
index 6e0063b..0000000
--- a/src/libc/stdio.c
+++ /dev/null
@@ -1,25 +0,0 @@
-#include "stdio.h"
-#include "string.h"
-
-extern VIDEO_STATE VS;
-
-void print(char *str){
- int i=0;
- while(str[i]!='\0'){
- putchar(str[i]);
- i++;
- }
-}
-
-void printc(char* str,VIDEO_COLORS c){
- VIDEO_COLORS backup=VS.fg;
- VS.fg=c;
- print(str);
- VS.fg=backup;
-}
-
-void printi(int i){
- char str[12];
- itoa(i,str);
- print(str);
-} \ No newline at end of file
diff --git a/src/libc/stdio.h b/src/libc/stdio.h
deleted file mode 100644
index d4bd6b6..0000000
--- a/src/libc/stdio.h
+++ /dev/null
@@ -1,21 +0,0 @@
-#ifndef STDIO_H
-#define STDIO_H
-
-#include "drivers/framebuffer.h"
-
-/**
- * Print a char* in the framebuffer
- */
-void print(char*);
-
-/**
- * Print a char in the framebuffer
- */
-void printc(char*,VIDEO_COLORS c);
-
-/**
- * Print an integer using itoa()
- */
-void printi(int i);
-
-#endif \ No newline at end of file
diff --git a/src/libc/string.c b/src/libc/string.c
deleted file mode 100644
index 93a9e63..0000000
--- a/src/libc/string.c
+++ /dev/null
@@ -1,29 +0,0 @@
-#include "string.h"
-#include "math.h"
-
-void itoa(int i, char *a){
- // Check if lower than 0
- char neg=0;
- if(i<0){
- neg=1;
- i=-i;
- a[0]='-';
- }
-
- // Count number of digits
- int len=1;
- while(i/pow(10,len)>=1)
- {
- len++;
- }
-
- // Build string
- int max_pow=len-1;
- for(int j=0;j<=max_pow;j++){
- int cur_pow=pow(10,max_pow-j);
- char digit=i/cur_pow;
- a[j+neg]='0'+digit;
- i=i-digit*cur_pow; // Remove first digits (most significant)
- }
- a[len+neg]='\0';
-} \ No newline at end of file
diff --git a/src/libc/string.h b/src/libc/string.h
deleted file mode 100644
index c3d3614..0000000
--- a/src/libc/string.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#ifndef STRING_H
-#define STRING_H
-
-/**
- * Convert int to char
- */
-void itoa(int i, char *a);
-
-#endif \ No newline at end of file
diff --git a/src/libs/math.cc b/src/libs/math.cc
new file mode 100644
index 0000000..63487b2
--- /dev/null
+++ b/src/libs/math.cc
@@ -0,0 +1,32 @@
+#include "math.hpp"
+
+int pow(int x, int n) {
+ if (n < 0)
+ return -1;
+ else if (n == 0)
+ return 1;
+ else if (n == 1)
+ return x;
+ int ret = x;
+ for (int i = 0; i < (n - 1); i++)
+ ret *= x;
+ return ret;
+}
+
+int max(int x, int y) {
+ if (x > y)
+ return x;
+ return y;
+}
+
+int min(int x, int y) {
+ if (x < y)
+ return x;
+ return y;
+}
+
+int abs(int x) {
+ if (x < 0)
+ return -x;
+ return x;
+}
diff --git a/src/libs/math.hpp b/src/libs/math.hpp
new file mode 100644
index 0000000..1eaac1c
--- /dev/null
+++ b/src/libs/math.hpp
@@ -0,0 +1,6 @@
+#pragma once
+
+int pow(int x, int n);
+int max(int x, int y);
+int min(int x, int y);
+int abs(int x);
diff --git a/src/libs/stdio.cc b/src/libs/stdio.cc
new file mode 100644
index 0000000..4a20c05
--- /dev/null
+++ b/src/libs/stdio.cc
@@ -0,0 +1,123 @@
+#include "stdio.hpp"
+#include "drivers/framebuffer.hpp"
+#include "math.hpp"
+#include "string.hpp"
+
+extern VIDEO_STATE VS;
+void (*__putchar)(char)=putchar;
+
+void printk(char *str,...) {
+ u64 rsi,rdx,rcx,r8,r9;
+ u64* rbp;
+ asm( "mov %%rsi, %0": "=a"(rsi));
+ asm( "mov %%rdx, %0": "=a"(rdx));
+ asm( "mov %%rcx, %0": "=a"(rcx));
+ asm( "mov %%r8, %0": "=a"(r8));
+ asm( "mov %%r9, %0": "=a"(r9));
+ asm( "mov %%rbp, %0": "=a"(rbp));
+
+ // Init informations
+ int len=strlen(str);
+ int i=0; // Pointer to the current character
+ int p=1; // Pointer to the current parameter
+
+ while (i!=len) {
+ char c=str[i];
+ // Check if special char is comming
+ if(str[i]=='%'){
+ char c2=str[i+1];
+ char c3=str[i+2];
+ // First just consider the data as a void pointer
+ u64 data;
+ switch (p) {
+ case 1:
+ data=rsi;
+ break;
+ case 2:
+ data=rdx;
+ break;
+ case 3:
+ data=rcx;
+ break;
+ case 4:
+ data=r8;
+ break;
+ case 5:
+ data=r9;
+ break;
+ default:
+ data=*(rbp+2+p-6);
+ }
+
+ if(c2=='%'){
+ __putchar('%');
+ i++;
+ }
+ else if (c2=='d') {
+ int data_int=(int)data;
+ printi(data_int);
+ i++;
+ p++;
+ }
+ else if (c2=='s') {
+ print((char*)data);
+ i++;
+ p++;
+ }
+ else if (c2=='x') {
+ print("0x");
+ printh(data);
+ i++;
+ p++;
+ }
+ else if (c2=='l' && c3=='l') {
+ printi(data); // TODO: Print 64bit number
+ i+=2;
+ p++;
+ }
+ }
+ else{
+ __putchar(c);
+ }
+ i++;
+ }
+}
+
+void print(char *s){
+ int i=0;
+ while(s[i]!='\0'){
+ __putchar(s[i]);
+ i++;
+ }
+}
+
+void printc(char *str, VIDEO_COLORS c) {
+ VIDEO_COLORS backup = (VIDEO_COLORS)VS.fg;
+ VS.fg = c;
+ print(str);
+ VS.fg = backup;
+}
+
+void printi(int i) {
+ char str[12];
+ itoa(i, str);
+ print(str);
+}
+
+void printh(int h) {
+ char str[17];
+ itoh(h, str);
+ print(str);
+}
+void printh(int h, int size) {
+ char str[17];
+ char str2[17];
+ itoh(h, str);
+ int a = 0;
+ for (int i = min(max(16 - size, 0), 15); i < 16; i++) {
+ str2[a] = str[i];
+ a++;
+ }
+ str2[a] = '\0';
+ print(str2);
+}
diff --git a/src/libs/stdio.hpp b/src/libs/stdio.hpp
new file mode 100644
index 0000000..f48ae81
--- /dev/null
+++ b/src/libs/stdio.hpp
@@ -0,0 +1,36 @@
+#pragma once
+
+#include "drivers/framebuffer.hpp"
+
+/// @brief Current active framebuffer driver
+extern void (*__putchar)(char);
+
+/**
+ * Print a char* in the framebuffer
+ */
+void printk(char *,...);
+
+/**
+ * Print a char*
+ */
+void print(char *s);
+
+/**
+ * Print a char in the framebuffer
+ */
+void printc(char *, VIDEO_COLORS c);
+
+/**
+ * Print an integer using itoa()
+ */
+void printi(int i);
+
+/**
+ * Print an integer as hex using itoh()
+ */
+void printh(int h);
+
+/**
+ * Print an integer as hex using itoh() truncated to size
+ */
+void printh(int h, int size);
diff --git a/src/libs/string.cc b/src/libs/string.cc
new file mode 100644
index 0000000..f8ae3ca
--- /dev/null
+++ b/src/libs/string.cc
@@ -0,0 +1,71 @@
+#include "string.hpp"
+#include "math.hpp"
+
+void memcpy(void* src, void* dst, int size){
+ char *c_src=(char*)src;
+ char *c_dst=(char*)dst;
+ for(int i=0;i<size;i++)
+ *c_dst=*c_src;
+}
+
+void itoa(u64 i, char *a){
+ // Check if lower than 0
+ char neg=0;
+ if(i<0){
+ neg=1;
+ i=-i;
+ a[0]='-';
+ }
+
+ // Count number of digits
+ int len=1;
+ while(i/pow(10,len)>=1)
+ {
+ len++;
+ }
+
+ // Build string
+ int max_pow=len-1;
+ for(int j=0;j<=max_pow;j++){
+ int cur_pow=pow(10,max_pow-j);
+ char digit=i/cur_pow;
+ a[j+neg]='0'+digit;
+ i=i-digit*cur_pow; // Remove first digits (most significant)
+ }
+ a[len+neg]='\0';
+}
+
+void itoh(u64 i, char *a){
+ char hex[]={'0','1','2','3','4','5','6','7','8','9',
+ 'A','B','C','D','E','F'
+ };
+
+ // i should be split int two
+ // indeed shifting with more than 32 bits seems undefined
+ u32 i_a=i&0xFFFFFFFF;
+ u32 i_b=i>>32;
+
+ for(char j=0;j<8;j++){
+ u64 t=(j*4);
+ u64 mask=0xF;
+ mask=mask << t;
+ u64 index=(i_a&mask) >> t;
+ a[15-j]=hex[index];
+ }
+
+ for(char j=0;j<8;j++){
+ u64 t=(j*4);
+ u64 mask=0xF;
+ mask=mask << t;
+ u64 index=(i_b&mask) >> t;
+ a[15-(j+8)]=hex[index];
+ }
+ a[16]='\0';
+}
+
+int strlen(char *s){
+ int i=0;
+ while(s[i]!='\0')
+ i++;
+ return i;
+} \ No newline at end of file
diff --git a/src/libs/string.hpp b/src/libs/string.hpp
new file mode 100644
index 0000000..305b9a8
--- /dev/null
+++ b/src/libs/string.hpp
@@ -0,0 +1,23 @@
+#pragma once
+
+#include "core/types.hpp"
+
+/**
+ * Copy data byte per byte from src to dst
+ */
+void memcpy(void *src, void *dst, int size);
+
+/**
+ * Convert int to char array
+ */
+void itoa(u64 i, char *a);
+
+/**
+ * Convert int to char array
+ */
+void itoh(u64 i, char *a);
+
+/**
+ * Length of a char*
+ */
+int strlen(char *s);
diff --git a/src/linker.ld b/src/linker.ld
index ed8fc5d..89e4909 100644
--- a/src/linker.ld
+++ b/src/linker.ld
@@ -7,7 +7,8 @@ SECTIONS {
.text : ALIGN(4)
{
- *(.multiboot)
+ *boot.o(.multiboot)
+ *boot.o(.text)
*(.text)
}
.rodata : ALIGN(4)