Programs: loading, 1 fake syscall.. (bad) #12

Merged
xamidev merged 9 commits from programs into main 2024-09-23 16:41:44 +02:00
9 changed files with 38 additions and 8 deletions
Showing only changes of commit af716cb2ec - Show all commits

BIN
hello.bin

Binary file not shown.

View File

@@ -1,5 +1,5 @@
CC = i386-elf-7.5.0-Linux-x86_64/bin/i386-elf-gcc CC = i386-elf-7.5.0-Linux-x86_64/bin/i386-elf-gcc
CFLAGS = -ffreestanding -g -Wall -Wextra -Wno-builtin-declaration-mismatch -mno-sse -mno-mmx -mno-avx -march=i386 -c -I src/ CFLAGS = -ffreestanding -g -Wall -Wextra -mno-sse -mno-mmx -mno-avx -march=i386 -c -I src/
LD = ld LD = ld
LDFLAGS = -T link.ld -melf_i386 LDFLAGS = -T link.ld -melf_i386
AS = nasm AS = nasm

View File

@@ -232,7 +232,7 @@ void* load_file_from_initrd(uint8_t* initrd, const char* filename)
void* file_content = tar_get_file_content(file); void* file_content = tar_get_file_content(file);
memcpy(file_data, file_content, file_size); memcpy(file_data, file_content, file_size);
printf("Loaded '%s' at 0x%x, size=%u\n", filename, (unsigned int)file_data, file_size); printf("[initrd] Loaded '%s' at 0x%x, size=%u\n", filename, (unsigned int)file_data, file_size);
return file_data; return file_data;
} }

View File

@@ -6,6 +6,7 @@
#include "kheap.h" #include "kheap.h"
#include <stdint.h> #include <stdint.h>
#include "system.h" #include "system.h"
#include "../libc/stdio.h"
// Free list allocator // Free list allocator
@@ -17,6 +18,7 @@ void init_alloc()
free_list = (block_t*)heap; free_list = (block_t*)heap;
free_list->size = HEAP_SIZE-sizeof(block_t); free_list->size = HEAP_SIZE-sizeof(block_t);
free_list->next = NULL; free_list->next = NULL;
printf("[kernel] initialized heap and allocator, start=0x%x\n", heap);
} }
void* malloc(size_t size) void* malloc(size_t size)

View File

@@ -17,7 +17,6 @@ typedef struct block
#define HEAP_SIZE 1024*1024 // 1MB malloc-able #define HEAP_SIZE 1024*1024 // 1MB malloc-able
void init_alloc(); void init_alloc();
void* malloc(size_t size); void* malloc(size_t size);
void free(void* ptr); void free(void* ptr);

View File

@@ -123,9 +123,9 @@ void kmain(multiboot2_info *mb_info)
void* binary_file = load_file_from_initrd((uint8_t*)initrd_addr, "./hello.bin"); void* binary_file = load_file_from_initrd((uint8_t*)initrd_addr, "./hello.bin");
if (binary_file == NULL) if (binary_file == NULL)
{ {
printf("NOT LOADED...\n"); printf("[debug] Failed to load test program!\n");
} else { } else {
printf("LOADED!\n"); printf("[debug] Test program loaded!\n");
} }
void (*program_entry)() = (void (*)())binary_file; void (*program_entry)() = (void (*)())binary_file;

20
src/kernel/syscall.c Normal file
View File

@@ -0,0 +1,20 @@
#include <stdarg.h>
#include "../libc/stdio.h"
#include "syscall.h"
syscall_t syscalls[] = {
(syscall_t)printf,
};
int syscall_handler(int syscall_num, ...)
{
if ((unsigned)syscall_num < sizeof(syscalls)/sizeof(syscall_t))
{
va_list args;
va_start(args, syscall_num);
syscalls[syscall_num](va_arg(args, const char*));
va_end(args);
return 0;
}
return -1;
}

8
src/kernel/syscall.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef SYSCALL_H
#define SYSCALL_H
#define SYSCALL_PRINTF 0
typedef void (*syscall_t)();
#endif

View File

@@ -1,8 +1,9 @@
#include "../libc/stdio.h" void user_printf(const char* format) {
#include "../drivers/serial.h" asm volatile ("int $0x80" : : "a"(1), "b"(format));
}
void main() void main()
{ {
serial_printf(3, "Hello, world, from a PROGRAM!\n"); user_printf("Hello, world, from a PROGRAM!\n");
return; return;
} }