Skip to content

Commit

Permalink
修复进程内存回收, 修改elf解析方式
Browse files Browse the repository at this point in the history
  • Loading branch information
VinbeWan committed Dec 7, 2024
2 parents 436f343 + e536ebb commit a8ad932
Show file tree
Hide file tree
Showing 51 changed files with 10,381 additions and 397 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CI Workflow

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

jobs:
build:
runs-on: ubuntu-latest
name: Build
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install packages
uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: xorriso mtools nasm
version: 1.0

- name: Setup BuildTools
uses: xmake-io/github-action-setup-xmake@v1
with:
xmake-version: latest
actions-cache-folder: '.xmake-cache'

- name: Build Kernel
run: xmake build -y

- uses: actions/upload-artifact@v4
with:
name: CoolPotOS
path: build/CoolPotOS.iso
compression-level: 9
17 changes: 10 additions & 7 deletions README-zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@

* `pl_readline` by min0911Y [plos-clan/pl_readline](https://github.com/plos-clan/pl_readline)
* `Uinxed-Mark` by ViudiraTech [ViudiraTech/Uinxed-Kernel](https://github.com/ViudiraTech/Uinxed-Kernel)

* `os_terminal` by wenxuanjun [plos-clan/libos-terminal](https://github.com/plos-clan/libos-terminal)
* `libelf_parser` by wenxuanjun [plos-clan/libelf_parser](https://github.com/plos-clan/libelf-parse)

## 构建并运行

### 环境

您需要安装这些软件:

- xmake
- zig工具链 & nasm汇编器
- 适用于Windows的Linux子系统 (Ubuntu 22.04)
- xorriso
- qemu-system-i386
- Xmake
- NASM
- Zig(如果 xmake 无法为您下载它,您可以手动安装)
- Windows subsystem for Linux (Ubuntu 22.04)
- xorriso
- qemu-system-i386

### 步骤

- 在终端上运行 `xmake run``bash build.bash`,项目将开始构建并运行
- 在终端上运行 `xmake run`,项目将开始构建并运行
- `init.bash` and `build.bash` 目前已弃用

## 许可协议

Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ This is a simple operating system for x86 architecture.

You need to install them on your computer:

- xmake toolkit
- zig cc & nasm
- rustup
- Xmake
- NASM
- Zig (you can install manually if xmake cannot download it for you)
- Windows subsystem for Linux (Ubuntu 22.04)
- xorriso
- qemu-system-i386

### Steps

- Run `xmake run` or `bash build.bash` on your terminal then it is going to build and run
- Run `xmake run` on your terminal then it will build and run automatically
- `init.bash` and `build.bash` are deprecated currently

## License

Expand Down
6 changes: 6 additions & 0 deletions assets/limine.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
timeout: 3

/CoolPotOS-i386
protocol: multiboot1
kernel_path: boot():/sys/cpkrnl.elf
cmdline: desktop
Binary file added assets/limine/limine-bios-cd.bin
Binary file not shown.
Binary file added assets/limine/limine-bios.sys
Binary file not shown.
Binary file added libs/libelf_parse.a
Binary file not shown.
Binary file added libs/libos_terminal.a
Binary file not shown.
4 changes: 4 additions & 0 deletions src/core/dlinker.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "dlinker.h"
#include "elf_util.h"


24 changes: 23 additions & 1 deletion src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include "scheduler.h"
#include "krlibc.h"
#include "syscall.h"
#include "speaker.h"
#include "hda.h"
#include "vsound.h"
#include "shell.h"

extern void* program_break_end;
Expand All @@ -38,6 +41,10 @@ _Noreturn void shutdown(){
while (1);
}

static void play_music(){
wav_player("/music_box.mp3");
}

/*
* 内核初始化函数, 最终会演变为CPU0的IDLE进程
* > 注意, 其所有的函数调用顺序均不可改变. 需按顺序初始化OS功能
Expand All @@ -53,10 +60,14 @@ _Noreturn void kernel_main(multiboot_t *multiboot, uint32_t kernel_stack) {
idt_install(); //8259A PIC初始化
tty_init(); //tty 设备初始化

default_terminal_setup();
extern void check_memory(multiboot_t *multiboot);
check_memory(multiboot);

init_vbe(multiboot);
page_init(multiboot); //分页开启
setup_free_page();
terminal_setup();
terminal_setup(false);
printk("CoolPotOS %s (Limine Multiboot) on an i386\n",KERNEL_NAME);
printk("KernelArea: 0x00000000 - 0x%08x | GraphicsBuffer: 0x%08x \n",
program_break_end,
Expand All @@ -71,6 +82,8 @@ _Noreturn void kernel_main(multiboot_t *multiboot, uint32_t kernel_stack) {

ide_init();
ahci_init();
hda_init();
hda_regist();

devfs_regist();

Expand All @@ -84,6 +97,13 @@ _Noreturn void kernel_main(multiboot_t *multiboot, uint32_t kernel_stack) {

setup_syscall();

// extern void f3system_setup();
// create_kernel_thread((void*)f3system_setup,NULL,"f3system");
// klogf(true,"Enable f3system service.\n");

create_kernel_thread((void*)play_music,NULL,"music");
klogf(true,"Enable music service.\n");

create_kernel_thread((void*)setup_shell, NULL, "Shell");
klogf(true,"Enable kernel shell service.\n");

Expand All @@ -103,6 +123,8 @@ _Noreturn void kernel_main(multiboot_t *multiboot, uint32_t kernel_stack) {
jmp:

klogf(true,"Kernel load done!\n");
//beep();
clock_sleep(100);
enable_scheduler();
io_sti(); //内核加载完毕, 打开中断以启动进程调度器, 开始运行

Expand Down
12 changes: 12 additions & 0 deletions src/core/mboot/memory_map.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
#include "multiboot.h"
#include "ctypes.h"
#include "klog.h"
#include "io.h"

uint32_t phy_mem_size;

void check_memory(multiboot_t *multiboot){
if ((multiboot->mem_upper + multiboot->mem_lower) / 1024 + 1 < 30) {
printk("CP_Kernel bootloader panic info:\n");
printk("\033ff0000;Minimal RAM amount for CP_Kernel is 30 MB, but you have only %d MB.\033c6c6c6;\n",
(multiboot->mem_upper + multiboot->mem_lower) / 1024 + 1);
printk("Please check your memory size, and restart CP_Kernel.\n");
while (1) io_hlt();
}
phy_mem_size = (multiboot->mem_upper + multiboot->mem_lower) / 1024;
}

void show_memory_map(multiboot_t *mboot) {
uint32_t mmap_addr = mboot->mmap_addr;
uint32_t mmap_length = mboot->mmap_length;
Expand Down
5 changes: 2 additions & 3 deletions src/core/memory/free_page.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ void free_pages(){
ii = fifo8_get(fifo8);
if(ii == -1) return;
page_directory_t *dir = (page_directory_t *)ii;
return;
for (int i = 0; i < 1024; i++) {
page_table_t *table = dir->tables[i];
for (int j = 0; j < 1024; j++) {
page_t page = table->pages[i];
free_frame(&page);
}
// kfree(table);
//kfree(table);
}
//kfree(dir);
kfree(dir);
} while (1);
}

Expand Down
1 change: 0 additions & 1 deletion src/core/memory/user_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ static void *sbrk(int incr){
pcb_t *pcb = get_current_proc();
if (pcb->program_break + incr >= pcb->program_break_end) {
ral:
if(pcb->program_break_end >= (void*)0x01bf8f7d) goto alloc_error; // 奇怪的界限, 不设置内核会卡死
if ((uint32_t) pcb->program_break_end < USER_AREA_START) {
uint32_t ai = (uint32_t) pcb->program_break_end;
for (; ai < (uint32_t) pcb->program_break_end + PAGE_SIZE * 10;) {
Expand Down
22 changes: 13 additions & 9 deletions src/core/task/pcb.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "description_table.h"
#include "elf_util.h"
#include "free_page.h"
#include "thread.h"

extern pcb_t *current_pcb;
extern pcb_t *running_proc_head;
Expand All @@ -30,7 +31,7 @@ static void add_task(pcb_t *new_task){ //添加进程至调度链
tailt->next = new_task;
}

static void switch_to_user_mode(uint32_t func) {
void switch_to_user_mode(uint32_t func) {
io_cli();
uint32_t esp = (uint32_t )get_current_proc()->user_stack + STACK_SIZE;
get_current_proc()->context.eflags = (0 << 12 | 0b10 | 1 << 9);
Expand Down Expand Up @@ -63,7 +64,7 @@ static void switch_to_user_mode(uint32_t func) {
"iret" ::"m"(a));
}

_Noreturn static void process_exit(){
_Noreturn void process_exit(){
register uint32_t eax __asm__("eax");
printk("Kernel Process exit, Code: %d\n",eax);
kill_proc(current_pcb);
Expand All @@ -84,12 +85,11 @@ int create_user_process(const char* path,const char* cmdline,char* name,uint8_t
new_task->pgd_dir = clone_directory(kernel_directory);
new_task->cpu_clock = 0;
new_task->tty = default_tty_alloc();
new_task->cpu_clock = 0;
new_task->exe_file = exefile;
new_task->kernel_stack = new_task;
new_task->sche_time = 1;
new_task->program_break = new_task->program_break_end = (void*)USER_AREA_START;

new_task->now_tid = 0;
// 映射形参数据区
new_task->program_break_end += PAGE_SIZE;
for (uint32_t i = (uint32_t)new_task->program_break; i < (uint32_t)new_task->program_break_end; i += PAGE_SIZE) {
Expand All @@ -114,13 +114,13 @@ int create_user_process(const char* path,const char* cmdline,char* name,uint8_t
disable_scheduler();
io_sti();
switch_page_directory(new_task->pgd_dir);
uint8_t *data = kmalloc(exefile->size);
Elf32_Ehdr *data = kmalloc(exefile->size);
if(vfs_read(exefile,data,0,exefile->size) == -1){
vfs_close(exefile);
return -1;
}
uint32_t _start = elf_load(exefile->size,data);
kfree(data);
uint32_t _start = load_elf(data,new_task->pgd_dir);
new_task->data = data;
switch_page_directory(cur_dir);
io_cli();
enable_scheduler();
Expand All @@ -132,7 +132,9 @@ int create_user_process(const char* path,const char* cmdline,char* name,uint8_t
return -1;
}

uint32_t *stack_top = (uint32_t * )((uint32_t) new_task + STACK_SIZE);
create_user_thread(new_task,(void*)_start);

uint32_t *stack_top = (uint32_t * )((uint32_t) new_task->kernel_stack + STACK_SIZE);
*(--stack_top) = (uint32_t) _start;
*(--stack_top) = (uint32_t) process_exit;
*(--stack_top) = (uint32_t) switch_to_user_mode;
Expand Down Expand Up @@ -161,7 +163,7 @@ int create_kernel_thread(int (*_start)(void* arg),void *args,char* name){ //创
new_task->cpu_clock = 0;
new_task->sche_time = 1;
new_task->user_stack = new_task->kernel_stack;

new_task->data = NULL;
new_task->kernel_stack = new_task;

uint32_t *stack_top = (uint32_t * )((uint32_t) new_task + STACK_SIZE);
Expand Down Expand Up @@ -201,6 +203,7 @@ void kill_proc(pcb_t *pcb){
if(pcb->task_level == TASK_KERNEL_LEVEL){

} else{
kfree(pcb->data);
vfs_close(pcb->exe_file);
put_directory(pcb->pgd_dir);
}
Expand Down Expand Up @@ -242,6 +245,7 @@ void init_pcb(){
current_pcb->sche_time = 1;
current_pcb->pgd_dir = kernel_directory;
current_pcb->context.esp = (uint32_t )current_pcb->kernel_stack;
current_pcb->cpu_clock = 0;

current_pcb->program_break = program_break;
current_pcb->program_break_end = program_break_end;
Expand Down
19 changes: 16 additions & 3 deletions src/core/task/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#include "description_table.h"
#include "klog.h"
#include "io.h"
#include "timer.h"

extern void switch_to(struct context *prev, struct context *next); //asmfunc.asm

pcb_t *current_pcb = NULL;
pcb_t *running_proc_head = NULL;
pcb_t *current_pcb = NULL; //当前运行进程
pcb_t *running_proc_head = NULL; //调度队列
pcb_t *wait_proc_head = NULL; //等待队列

bool can_sche = false; //调度标志位

int get_all_task(){
Expand All @@ -34,11 +37,21 @@ pcb_t *get_current_proc(){
return current_pcb;
}

void kernel_sche(){
__asm__("int $31\n");
}

void scheduler_process(registers_t *reg){
io_cli();
if(current_pcb && can_sche){
current_pcb->cpu_clock++;
default_scheduler(reg,current_pcb->next);
if(current_pcb->task_level == TASK_KERNEL_LEVEL){
default_scheduler(reg,current_pcb->next);
} else{
//TODO 用户线程调度
default_scheduler(reg,current_pcb->next);
}

}
}

Expand Down
30 changes: 29 additions & 1 deletion src/core/task/thread.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
#include "thread.h"
#include "krlibc.h"
#include "user_malloc.h"

int get_pcb_threads(pcb_t *pcb){
static void add_thread(pcb_t *pcb,cp_thread_t thread){ //添加线程至调度链
if(pcb == NULL || thread == NULL) return;
cp_thread_t tailt = pcb->thread_head;
while (tailt->next != pcb->thread_head) {
if(tailt->next == NULL) break;
tailt = tailt->next;
}
tailt->next = thread;
}

void create_user_thread(pcb_t *pcb,void* func){
cp_thread_t thread = kmalloc(STACK_SIZE);
thread->next = NULL;
thread->father_pcb = pcb;
thread->tid = pcb->now_tid++;
thread->kernel_stack = thread;
thread->user_stack = pcb->user_stack;

uint32_t *stack_top = (uint32_t * )((uint32_t) thread->kernel_stack + STACK_SIZE);
*(--stack_top) = (uint32_t) func;
*(--stack_top) = (uint32_t) process_exit;
*(--stack_top) = (uint32_t) switch_to_user_mode;
thread->context.esp = (uint32_t) thread + STACK_SIZE - sizeof(uint32_t) * 3;
thread->context.eflags = 0x200;

if(pcb->thread_head == NULL){
pcb->thread_head = thread;
}else add_thread(pcb,thread);
}
1 change: 0 additions & 1 deletion src/core/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "vfs.h"
#include "list.h"


vfs_node_t rootdir = NULL;

static void empty_func() {}
Expand Down
Loading

0 comments on commit a8ad932

Please sign in to comment.