Add: libc

This commit is contained in:
brice.boisson 2023-09-27 10:35:00 +09:00
parent 62837e9c44
commit 633decc0af
12 changed files with 85 additions and 11 deletions

View File

@ -1,6 +1,6 @@
include Makefile.common include Makefile.common
all: k.iso all: bin k.iso
k.iso: install k.iso: install
./tools/create_iso.sh ./tools/create_iso.sh
@ -9,8 +9,16 @@ install:
mkdir -p $(GRUBDIR) mkdir -p $(GRUBDIR)
$(MAKE) -C $(SOURCEDIR) $@ $(MAKE) -C $(SOURCEDIR) $@
bin:
$(MAKE) -C $(LIB)
gcc -c test.c -Ilibk -Llibk -lk -m32
ld -m elf_i386 -Ttext=0x6000000 --entry=main test.o -L./libk/ -lk
objcopy --input binary --output elf32-i386 --binary-architecture i386 --rename-section .data=.rodata,CONTENTS,ALLOC,LOAD,READONLY,DATA a.out myfile.o
cp myfile.o $(SOURCEDIR)/myfile.o
clean: clean:
$(MAKE) -C $(SOURCEDIR) $@ $(MAKE) -C $(SOURCEDIR) $@
$(MAKE) -C $(LIB) $@
$(RM) kernel.iso $(RM) kernel.iso
$(RM) -r iso $(RM) -r iso

View File

@ -4,3 +4,4 @@ ASFLAGS = -m32
GRUBDIR = iso GRUBDIR = iso
SOURCEDIR = src SOURCEDIR = src
TARGET = kernel TARGET = kernel
LIB=libk

20
libk/Makefile Normal file
View File

@ -0,0 +1,20 @@
include ../Makefile.common
OBJS = \
syscall.o \
DEPS = $(OBJS:.o=.d)
CPPFLAGS += -MMD -Iinclude
CFLAGS += $(K_EXTRA_CFLAGS) -g -nostdlib
LDFLAGS += -Wl,-Tkernel.lds
LDLIBS =
all: $(TARGET)
ar -rcs $(LIB).a *.o
$(TARGET): $(OBJS)
clean:
$(RM) $(OBJS) $(DEPS) $(TARGET) $(LIB).a

27
libk/syscall.c Normal file
View File

@ -0,0 +1,27 @@
#include "syscall.h"
int __syscall(int num, void *a0, void *a1, void *a2, void *a3, void *a4)
{
int ret;
asm volatile(" \n \
mov %1, %%eax; \n \
mov %2, %%ebx; \n \
mov %3, %%ecx; \n \
mov %4, %%edx; \n \
mov %5, %%edi; \n \
mov %6, %%esi; \n \
int $48; \n \
mov %%eax, %0" : "=m" (ret),
"+m" (num),
"+m" (a0),
"+m" (a1),
"+m" (a2),
"+m" (a3),
"+m" (a4));
return ret;
}
int write(int fd, char *buf, int count)
{
return __syscall(1, (void *) fd, (void *) buf, (void *) count, (void *) 0, (void *) 0);
}

7
libk/syscall.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef SYSCALL_H
#define SYSCALL_H
int __syscall(int num, void *a0, void *a1, void *a2, void *a3, void *a4);
int write(int fd, char *buf, int count);
#endif /* !SYSCALL_H */

View File

@ -87,11 +87,19 @@ _asm_sycall_handler:
mov $0x10,%bx mov $0x10,%bx
mov %bx,%ds mov %bx,%ds
pop %ebx pop %ebx
push %esi
push %edi
push %edx
push %ecx
push %ebx push %ebx
push %eax push %eax
call syscall_handler call syscall_handler
pop %ebx pop %ebx
pop %ebx pop %ebx
pop %ecx
pop %edx
pop %edi
pop %esi
pop %gs pop %gs
pop %fs pop %fs
pop %es pop %es

View File

@ -28,16 +28,17 @@ void isr_kbd_int(void)
DEBUG_INFO("Keyboard input: %d", x); DEBUG_INFO("Keyboard input: %d", x);
} }
void syscall_handler(int eax, int ebx) int syscall_handler(int eax, void *ebx, void *ecx, void *edx, void *edi, void *esi)
{ {
DEBUG_INFO("Syscall %d has been called from the userland with parameter %d", eax, ebx); DEBUG_INFO("Syscall %d has been called from the userland with parameters: %d, %d, %d, %d, %d", eax, ebx, ecx, edx, edi, esi);
switch (eax) switch (eax)
{ {
case 1: case 1:
char t = *((char *) ebx); int fd = (int) ebx;
DEBUG_INFO("Syscall write : %d", (int) t); char *buf = (char *) ecx;
return write(1, (u32) ebx); int size = (int) edx;
return write(fd, buf, size);
break; break;
case 2: case 2:
DEBUG_INFO("Syscall keyboard"); DEBUG_INFO("Syscall keyboard");

View File

@ -24,7 +24,7 @@ void launch_process(u16 tss, int memory_start, int userland_stack, int userland_
movw %%ax, %%gs \n \ movw %%ax, %%gs \n \
mov %%esp, %%eax \n \ mov %%esp, %%eax \n \
push $0x23 \n \ push $0x23 \n \
push $0x7FFFFFFF \n \ push $0x7FFFEFFF \n \
pushfl \n \ pushfl \n \
push $0x1B \n \ push $0x1B \n \
push $0x6000000 \n \ push $0x6000000 \n \

Binary file not shown.

View File

@ -6,9 +6,9 @@
* Syscall handler for write, currently only serial write is supported. * Syscall handler for write, currently only serial write is supported.
* Use the fd 1, to make a serial write. * Use the fd 1, to make a serial write.
*/ */
int write(int fd, void *buf) int write(int fd, void *buf, int size)
{ {
if (fd != 1) if (fd != 1 || size < 0)
return -1; return -1;
if (write_serial(buf)) if (write_serial(buf))

View File

@ -1,7 +1,7 @@
#ifndef SYSCALL_H #ifndef SYSCALL_H
#define SYSCALL_H #define SYSCALL_H
int write(int fd, void *buf); int write(int fd, void *buf, int size);
int keyboard(void); int keyboard(void);
#endif /* !SYSCALL_H */ #endif /* !SYSCALL_H */

View File

@ -20,6 +20,8 @@ int create_process(int uid, char *data_start)
// Allocate stack // Allocate stack
allocate_new_page(uid, 0x7FFFF000); allocate_new_page(uid, 0x7FFFF000);
allocate_new_page(uid, 0x7FFFE000);
allocate_new_page(uid, 0x7FFFD000);
// TODO : create data seg by process // TODO : create data seg by process
// load elf // load elf