Skip to content

Commit

Permalink
更新多任务与elf加载
Browse files Browse the repository at this point in the history
  • Loading branch information
copi143 committed Jan 11, 2025
1 parent d3a754a commit 749d510
Show file tree
Hide file tree
Showing 22 changed files with 133 additions and 124 deletions.
11 changes: 0 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ set_optim_flag("-g -O3") # 默认参数,请一直开启调试符号
# set_optim_flag("-O3") # 发布开 O3
# set_optim_flag("-Ofast") #

# add_compile_flag("-w") # 如果你不想编译器输出 warning
add_link_flag("-w") # 可恶的链接器警告

add_compile_flag("-fvisibility=hidden") # 隐藏符号

# 目标环境
add_compile_flag("-march=native")

Expand Down Expand Up @@ -95,11 +90,6 @@ set_optim_flag("-g -O3") # 默认参数,请一直开启调试符号
# set_optim_flag("-O3") # 测试通过
# set_optim_flag("-Ofast") #

# add_compile_flag("-w") # 如果你不想编译器输出 warning
add_link_flag("-w") # 可恶的链接器警告

add_compile_flag("-fvisibility=hidden") # 隐藏符号

# 目标环境
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_compile_flag("-target i386-none-elf")
Expand Down Expand Up @@ -127,7 +117,6 @@ add_compile_flag("-fno-delete-null-pointer-checks") # 不删除程序员进行
add_compile_flag("-fno-strict-aliasing") # 禁用严格别名规则 (gcc 默认关闭)
add_compile_flag("-finput-charset=UTF-8 -fexec-charset=UTF-8") # 使用 utf-8 (默认)
add_compile_flag("-fno-stack-protector") # 不使用栈保护 (默认使用)
add_compile_flag("-fno-pic -fno-pie") # 不使用 pic 和 pie
add_compile_flag("-fno-asynchronous-unwind-tables")
add_compile_flag("-fno-exceptions")

Expand Down
6 changes: 3 additions & 3 deletions cmake/code-gc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# 删除重复代码
add_compile_flag("-ffunction-sections -fdata-sections")
add_link_flag("-Wl,--gc-sections")
add_compile_flag("-ffunction-sections -fdata-sections") # 删除重复代码
add_link_flag("-Wl,--gc-sections") # 删除重复代码
add_compile_flag("-fvisibility=hidden") # 隐藏符号
3 changes: 3 additions & 0 deletions cmake/disable-warning
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
endif()
add_compile_flag("-Wno-address-of-packed-member")
add_compile_flag("-Wno-attributes")

# add_compile_flag("-w") # 如果你不想编译器输出 warning
add_link_flag("-w") # 可恶的链接器警告
5 changes: 5 additions & 0 deletions cmake/optim
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# 优化选项

add_compile_flag("-fpie") # 生成位置无关代码

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_compile_flag("-fmodulo-sched") # 在第一次调度过程之前立即执行摇摆模调度 (gcc 默认关闭)
add_compile_flag("-fsplit-loops") # 对于循环内的 if 语句,将其分割为两个循环 (gcc 默认关闭)
Expand All @@ -10,6 +13,7 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_compile_flag("-fipa-icf") # 对函数和只读变量执行相同的代码折叠 (gcc O2)
add_compile_flag("-fipa-pta") # 过程间指针分析 (gcc 默认关闭)
endif()

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16)
add_compile_flag("-fcx-limited-range") # 启用涉及复杂算术运算的基本代数扩展 (?)
Expand All @@ -30,6 +34,7 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_compile_flag("-fwhole-program-vtables") # 启用整个程序 vtable 优化
add_compile_flag("-fwrapv") # 将有符号整数溢出视为二进制补码
endif()

add_compile_flag("-fmerge-all-constants") # 合并所有常量
add_compile_flag("-fno-keep-static-consts") # 不保留 static 常量
add_compile_flag("-fomit-frame-pointer") # 删除小函数的栈帧 (gcc O1)
Expand Down
1 change: 1 addition & 0 deletions doc/src/.pages
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ nav:
- 历史: history.md
- 编译: compile.md
- 内核: kernel
- 应用程序: app
26 changes: 26 additions & 0 deletions doc/src/app/memory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# 应用程序内存布局

## 32 位

`0000 0000 - 6fff ffff` 内核空间
`7000 0000 - efff ffff` 用户空间
`f000 0000 - ffff ffff` 内核共享数据

- `70000000 - 8fffffff` 代码段、数据段、bss 段
- `90000000 - afffffff`
- `b0000000 - dfffffff` mmap 区域
- `e0000000 - efffffff`
- `f0000000 - ffffffff` 内核共享数据

## 64 位

`0000 0000 0000 - 7fff ffff ffff` 内核空间
`8000 0000 0000 - efff ffff ffff` 用户空间
`f000 0000 0000 - ffff ffff ffff` 内核共享数据

- `8000 0000 0000 - 8fff ffff ffff` 代码段、数据段、bss 段
- `9000 0000 0000 - 9fff ffff ffff`
- `a000 0000 0000 - dfff ffff ffff` mmap 区域
- `e000 0000 0000 - efff ffff ffff`
- `f000 0000 0000 - ffff ffff ffff` 内核共享数据
2 changes: 1 addition & 1 deletion include/define/config/plos.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#define SYSCALL_USE_INTERRPUT 0

#define PLOS_LOGGING 2
#define PLOS_LOGGING 0

#define PLOS_LOGGING_PRINTS 0

Expand Down
2 changes: 0 additions & 2 deletions include/define/define/attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,3 @@
#define FASTCALL __attr(fastcall)

#define vectorize __attr(vectorize(enable))

#define where(expr) __attr(assume(expr))
2 changes: 1 addition & 1 deletion include/fs/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ int vfs_regist(cstr name, vfs_callback_t callback);
#define PATH_MAX 4096 // 路径最大长度
#define FILENAME_MAX 256 // 文件名最大长度

vfs_node_t vfs_open(cstr str);
vfs_node_t vfs_open(cstr _path);

/**
*\brief 创建文件夹
Expand Down
2 changes: 0 additions & 2 deletions include/kernel/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
#include "../elf.h"

bool elf32_is_validate(const Elf32Header *hdr);
void elf32_load_data(const Elf32Header *elf, byte *ptr);
u32 elf32_get_max_vaddr(const Elf32Header *hdr);
usize load_elf(const Elf32Header *elf);
24 changes: 24 additions & 0 deletions include/kernel/mtask.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ typedef struct task {
// AVX 4096
} *task_t;

#ifdef __x86_64__
# define ADDR_TASK_CODE ((usize)0x800000000000) // 代码段、数据段、bss 段
# define ADDR_TASK_HEAP ((usize)0x900000000000) // 堆
# define ADDR_TASK_MMAP ((usize)0xa00000000000) // mmap 区域
# define ADDR_TASK_STACK ((usize)0xe00000000000) // 栈
# define ADDR_TASK_SHARE ((usize)0xf00000000000) // 内核共享数据
# define SIZE_TASK_CODE (ADDR_TASK_HEAP - ADDR_TASK_CODE)
# define SIZE_TASK_HEAP (ADDR_TASK_MMAP - ADDR_TASK_HEAP)
# define SIZE_TASK_MMAP (ADDR_TASK_STACK - ADDR_TASK_MMAP)
# define SIZE_TASK_STACK (ADDR_TASK_SHARE - ADDR_TASK_STACK)
# define SIZE_TASK_SHARE (0 - ADDR_TASK_SHARE)
#else
# define ADDR_TASK_CODE ((usize)0x70000000) // 代码段、数据段、bss 段
# define ADDR_TASK_HEAP ((usize)0x90000000) // 堆
# define ADDR_TASK_MMAP ((usize)0xb0000000) // mmap 区域
# define ADDR_TASK_STACK ((usize)0xe0000000) // 栈
# define ADDR_TASK_SHARE ((usize)0xf0000000) // 内核共享数据
# define SIZE_TASK_CODE (ADDR_TASK_HEAP - ADDR_TASK_CODE)
# define SIZE_TASK_HEAP (ADDR_TASK_MMAP - ADDR_TASK_HEAP)
# define SIZE_TASK_MMAP (ADDR_TASK_STACK - ADDR_TASK_MMAP)
# define SIZE_TASK_STACK (ADDR_TASK_SHARE - ADDR_TASK_STACK)
# define SIZE_TASK_SHARE (0 - ADDR_TASK_SHARE)
#endif

extern task_t current_task;

extern task_t fpu_using_task; // 当前 fpu 上下文所属的进程
Expand Down
8 changes: 6 additions & 2 deletions include/kernel/syscall.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#pragma once
#include <sys/plos-syscall.h>

#ifdef __x86_64__
typedef isize (*syscall_t)(isize, isize, isize, isize, isize, isize);
#else
typedef isize (*syscall_t)(isize, isize, isize, isize, isize);
#endif

void syscall_exit(int status);
int syscall_fork();
void *syscall_getheap();
u32 syscall_heapsize();
void *syscall_mmap(void *start, u32 length);
void syscall_munmap(void *start, u32 length);
ssize_t syscall_read(int fd, void *addr, size_t size);
Expand Down
2 changes: 0 additions & 2 deletions include/kernel/task/kernel-part
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ struct _NAME {
ThreadState state; //
uint64_t jiffies; // 最后一次执行的全局时间片
task_t parent; //
u32 alloc_addr; //
u32 *alloc_size; //
cir_queue8_t press_key_fifo; // 基本输入设备的缓冲区
cir_queue8_t release_keyfifo; // 基本输入设备的缓冲区
cir_queue8_t keyfifo; // 基本输入设备的缓冲区
Expand Down
1 change: 1 addition & 0 deletions include/kernel/task/user-part
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct _NAME {
u32 signal_mask; //
timespec starttime; // 程序开始运行的时间
timespec cputime; // 程序实际运行的 CPU 时间
usize heapsize;
};

#undef _NAME
8 changes: 4 additions & 4 deletions src/fs/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ static vfs_node_t vfs_do_search(vfs_node_t dir, cstr name) {
return list_first(dir->child, data, streq(name, ((vfs_node_t)data)->name));
}

vfs_node_t vfs_open(cstr str) {
if (str == null) return null;
if (str[1] == '\0') return rootdir;
char *path = strdup(str + 1);
vfs_node_t vfs_open(cstr _path) {
if (_path == null) return null;
if (_path[1] == '\0') return rootdir;
char *path = strdup(_path + 1);
if (path == null) return null;

char *save_ptr = path;
Expand Down
41 changes: 8 additions & 33 deletions src/kernel/exec/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,25 @@ bool elf32_is_validate(const Elf32Header *hdr) {
return hdr->ident.magic == ELF_MAGIC;
}

void print_section_names(const Elf32Header *hdr) {
val shdr = (const Elf32SectionHeader *)((usize)hdr + hdr->shoff);
val strtab = (cstr)hdr + shdr[hdr->shstrndx].offset;
void print_section_names(const Elf32Header *elf) {
val shdr = (const Elf32SectionHeader *)((usize)elf + elf->shoff);
val strtab = (cstr)elf + shdr[elf->shstrndx].offset;
klogd("Section Headers:");
for (int i = 0; i < hdr->shnum; i++) {
for (int i = 0; i < elf->shnum; i++) {
klogd(" [%2d] %s", i, strtab + shdr[i].name);
}
}

u32 elf32_get_max_vaddr(const Elf32Header *hdr) {
var phdr = (const Elf32ProgramHeader *)((usize)hdr + hdr->phoff);
usize max = 0;
for (usize i = 0; i < hdr->phnum; i++) {
// 如果memsz大于filesz 说明这是bss段,我们以最大的为准
usize size = max(phdr[i].filesz, phdr[i].memsz);
max = max(max, phdr[i].vaddr + size);
}
return max;
}

void load_segment(const Elf32ProgramHeader *prog, const void *elf) {
if (prog->type != ELF_PROGRAM_TYPE_LOAD) return;
size_t hi = PADDING_UP(prog->paddr + prog->memsz, PAGE_SIZE);
size_t lo = PADDING_DOWN(prog->paddr, PAGE_SIZE);
if (prog->memsz < prog->filesz) return;
val hi = PADDING_UP(prog->vaddr + prog->memsz, PAGE_SIZE);
val lo = PADDING_DOWN(prog->vaddr, PAGE_SIZE);
for (size_t i = lo; i < hi; i += PAGE_SIZE) {
page_link(i);
}
memcpy((void *)prog->vaddr, elf + prog->offset, prog->filesz);
if (prog->memsz > prog->filesz) { // 这个是bss段
memset((void *)(prog->vaddr + prog->filesz), 0, prog->memsz - prog->filesz);
}
memset((void *)prog->vaddr + prog->filesz, 0, prog->memsz - prog->filesz);
}

usize load_elf(const Elf32Header *elf) {
Expand All @@ -45,16 +33,3 @@ usize load_elf(const Elf32Header *elf) {
print_section_names(elf);
return elf->entry;
}

void elf32_load_data(const Elf32Header *elf, byte *ptr) {
val p = (const byte *)elf;
for (usize i = 0; i < elf->shnum; i++) {
val section = (const Elf32SectionHeader *)(p + elf->shoff) + i;

if (section->type != SHT_PROGBITS || !(section->flags & SHF_ALLOC)) continue;

memcpy(ptr + section->addr, p + section->offset, section->size);
}

print_section_names(elf);
}
19 changes: 5 additions & 14 deletions src/kernel/exec/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ void task_app() {
cir_queue8_init(mfifo, 4096, mbuf);

task_set_fifo(current_task, kfifo, mfifo);
current_task->alloc_size = (u32 *)malloc(4);
*(current_task->alloc_size) = 2 * 1024 * 1024;
u32 pde = current_task->cr3;
u32 pde = current_task->cr3;
asm_cli;
asm_set_cr3(PD_ADDRESS);
current_task->cr3 = PD_ADDRESS;
Expand Down Expand Up @@ -108,19 +106,12 @@ void task_to_user_mode_elf() {
task_exit(I32_MAX);
__builtin_unreachable();
}
u32 alloc_addr = (elf32_get_max_vaddr((Elf32Header *)elf_data) & 0xfffff000) + PAGE_SIZE;
klogd("alloc_addr = %08x", alloc_addr);

u32 pg = PADDING_UP(*(current_task->alloc_size), PAGE_SIZE) / PAGE_SIZE;
for (int i = 0; i < pg + STACK_SIZE; i++) {
page_link(alloc_addr + i * PAGE_SIZE);
for (usize i = 1; i <= STACK_SIZE; i++) {
page_link(0xf0000000 - i * PAGE_SIZE);
}
u32 alloced_esp = alloc_addr + STACK_SIZE * PAGE_SIZE;
alloc_addr += STACK_SIZE * PAGE_SIZE;
iframe->esp = alloced_esp;
current_task->alloc_addr = alloc_addr;
current_task->v86_mode = 0;
iframe->eip = load_elf((Elf32Header *)elf_data);
iframe->esp = 0xf0000000 - STACK_SIZE;
iframe->eip = load_elf((Elf32Header *)elf_data);
klogd("eip = %08x", iframe->eip);
current_task->user_mode = 1;
tss.esp0 = current_task->stack_bottom;
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/io/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void printchar(char ch) {
tty_default->print(tty_default, ch1);
}

static char eos[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'f', 'J', 'K', 'S', 'T', 'm'};
static const char eos[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'f', 'J', 'K', 'S', 'T', 'm'};
// end of string,
// vt100控制字符中可能的结束符号

Expand Down
4 changes: 2 additions & 2 deletions src/kernel/syscall/read-write.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
// close,
// };

ssize_t syscall_read(int fd, void *addr, size_t size) {
isize syscall_read(int fd, void *addr, usize size) {
if (fd < 0) return -1;
if (addr == null) return -1;
return 0;
}

ssize_t syscall_write(int fd, const void *addr, size_t size) {
isize syscall_write(int fd, const void *addr, usize size) {
if (fd < 0) return -1;
if (addr == null) return -1;
return 0;
Expand Down
Loading

0 comments on commit 749d510

Please sign in to comment.