Skip to content

Commit

Permalink
✨ 130 一些优化 - 更新初始化方式
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenBaby committed Apr 6, 2023
1 parent 450df47 commit b7988b0
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 130 deletions.
5 changes: 5 additions & 0 deletions docs/05 内存管理/130 一些优化.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 一些优化

## 更新初始化的方式

将需要中断功能的设备初始化放到 init 进程中,此使系统已经支持中断功能。一些设备初始化时必须要中断(比如软盘),使得后续功能尽可能一致。
2 changes: 2 additions & 0 deletions src/include/onix/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ typedef enum std_fd_t

typedef int32 off_t; // 文件偏移

typedef int err_t; // 错误类型

#endif
29 changes: 13 additions & 16 deletions src/kernel/ide.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static void ide_handler(int vector)
}
}

static u32 ide_error(ide_ctrl_t *ctrl)
static void ide_error(ide_ctrl_t *ctrl)
{
u8 error = inb(ctrl->iobase + IDE_ERR);
if (error & IDE_ER_BBK)
Expand Down Expand Up @@ -270,12 +270,9 @@ int ide_pio_read(ide_disk_t *disk, void *buf, u8 count, idx_t lba)
for (size_t i = 0; i < count; i++)
{
task_t *task = running_task();
if (task->state == TASK_RUNNING) // 系统初始化时,不能使用异步方式
{
// 阻塞自己等待中断的到来,等待磁盘准备数据
ctrl->waiter = task;
task_block(task, NULL, TASK_BLOCKED, TIMELESS);
}
// 阻塞自己等待中断的到来,等待磁盘准备数据
ctrl->waiter = task;
assert(task_block(task, NULL, TASK_BLOCKED, TIMELESS) == EOK);

ide_busy_wait(ctrl, IDE_SR_DRQ);

Expand Down Expand Up @@ -317,12 +314,11 @@ int ide_pio_write(ide_disk_t *disk, void *buf, u8 count, idx_t lba)
ide_pio_write_sector(disk, (u16 *)offset);

task_t *task = running_task();
if (task->state == TASK_RUNNING) // 系统初始化时,不能使用异步方式
{
// 阻塞自己等待磁盘写数据完成
ctrl->waiter = task;
task_block(task, NULL, TASK_BLOCKED, TIMELESS);
}

// 阻塞自己等待磁盘写数据完成
ctrl->waiter = task;
assert(task_block(task, NULL, TASK_BLOCKED, TIMELESS) == EOK);

ide_busy_wait(ctrl, IDE_SR_NULL);
}

Expand Down Expand Up @@ -548,14 +544,15 @@ static void ide_install()
void ide_init()
{
LOGK("ide init...\n");
ide_ctrl_init();

ide_install(); // 安装设备

// 注册硬盘中断,并打开屏蔽字
set_interrupt_handler(IRQ_HARDDISK, ide_handler);
set_interrupt_handler(IRQ_HARDDISK2, ide_handler);
set_interrupt_mask(IRQ_HARDDISK, true);
set_interrupt_mask(IRQ_HARDDISK2, true);
set_interrupt_mask(IRQ_CASCADE, true);

ide_ctrl_init();

ide_install(); // 安装设备
}
23 changes: 23 additions & 0 deletions src/kernel/idle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <onix/interrupt.h>
#include <onix/syscall.h>
#include <onix/debug.h>

// #include <asm/unistd_32.h>

#define LOGK(fmt, args...) DEBUGK(fmt, ##args)

void idle_thread()
{
set_interrupt_state(true);
u32 counter = 0;
while (true)
{
// LOGK("idle task.... %d\n", counter++);
// BMB;
asm volatile(
"sti\n" // 开中断
"hlt\n" // 关闭 CPU,进入暂停状态,等待外中断的到来
);
yield(); // 放弃执行权,调度执行其他任务
}
}
77 changes: 77 additions & 0 deletions src/kernel/init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <onix/types.h>
#include <onix/syscall.h>
#include <onix/stdio.h>
#include <onix/task.h>

// 进入用户态,准备内核栈
// 保证栈顶占用足够的大小
// 约等于 char temp[100];
static void prepare_stack()
{
// 获取返回地址,也就是下面调用 task_to_user_mode 的位置
void *addr = __builtin_return_address(0);
asm volatile(
"subl $100, %%esp\n" // 为栈顶有足够的空间
"pushl %%eax\n" // 将返回地址压入栈中
"ret \n" // 直接返回
::"a"(addr));
}

extern int main();

int init_user_thread()
{
while (true)
{
u32 status;
pid_t pid = fork();
if (pid)
{
pid_t child = waitpid(pid, &status);
printf("wait pid %d status %d %d\n", child, status, time());
}
else
{
main();
}
}
return 0;
}

extern void serial_init();
extern void keyboard_init();
extern void time_init();
extern void tty_init();
extern void rtc_init();

extern void ide_init();
extern void ramdisk_init();

extern void buffer_init();
extern void file_init();
extern void inode_init();
extern void super_init();
extern void dev_init();

void init_thread()
{
serial_init(); // 初始化串口
keyboard_init(); // 初始化键盘
time_init(); // 初始化时间
tty_init(); // 初始化 TTY 设备,必须在键盘之后
// rtc_init(); // 初始化实时时钟,目前没用

ramdisk_init(); // 初始化内存虚拟磁盘

ide_init(); // 初始化 IDE 设备

buffer_init(); // 初始化高速缓冲
file_init(); // 初始化文件
inode_init(); // 初始化 inode
super_init(); // 初始化超级块

dev_init(); // 初始化设备文件

prepare_stack(); // 准备栈顶
task_to_user_mode(); // 进入用户态
}
51 changes: 12 additions & 39 deletions src/kernel/main.c
Original file line number Diff line number Diff line change
@@ -1,56 +1,29 @@

#include <onix/debug.h>
#include <onix/interrupt.h>

#define LOGK(fmt, args...) DEBUGK(fmt, ##args)

extern void tss_init();
extern void memory_map_init();
extern void mapping_init();
extern void arena_init();

extern void interrupt_init();
extern void clock_init();
extern void timer_init();
extern void time_init();
extern void rtc_init();
extern void keyboard_init();
extern void serial_init();
extern void tty_init();
extern void ide_init();
extern void ramdisk_init();
extern void task_init();
extern void syscall_init();
extern void tss_init();
extern void buffer_init();
extern void file_init();
extern void super_init();
extern void inode_init();
extern void hang();
extern void task_init();

void kernel_init()
{
tss_init();
memory_map_init();
mapping_init();
arena_init();

interrupt_init();
clock_init();
timer_init();
keyboard_init();
tty_init();
time_init();
serial_init();
// rtc_init();
ide_init();
ramdisk_init();
tss_init(); // 初始化任务状态段
memory_map_init(); // 初始化物理内存数组
mapping_init(); // 初始化内存映射
arena_init(); // 初始化内核堆内存

syscall_init();
task_init();
interrupt_init(); // 初始化中断
timer_init(); // 初始化定时器
clock_init(); // 初始化时钟

buffer_init();
file_init();
inode_init();
super_init();
syscall_init(); // 初始化系统调用
task_init(); // 初始化任务

set_interrupt_state(true);
}
4 changes: 1 addition & 3 deletions src/kernel/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ bool _inline task_leader(task_t *task)
}

// 任务阻塞
int task_block(task_t *task, list_t *blist, task_state_t state, int timeout_ms)
err_t task_block(task_t *task, list_t *blist, task_state_t state, int timeout_ms)
{
assert(!get_interrupt_state());
assert(task->node.next == NULL);
Expand Down Expand Up @@ -606,7 +606,6 @@ static void task_setup()

extern void idle_thread();
extern void init_thread();
extern void test_thread();

void task_init()
{
Expand All @@ -617,5 +616,4 @@ void task_init()

idle_task = task_create(idle_thread, "idle", 1, KERNEL_USER);
task_create(init_thread, "init", 5, NORMAL_USER);
task_create(test_thread, "test", 5, NORMAL_USER);
}
71 changes: 0 additions & 71 deletions src/kernel/thread.c

This file was deleted.

3 changes: 2 additions & 1 deletion src/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ $(BUILD)/kernel.bin: \
$(BUILD)/kernel/debug.o \
$(BUILD)/kernel/global.o \
$(BUILD)/kernel/task.o \
$(BUILD)/kernel/thread.o \
$(BUILD)/kernel/init.o \
$(BUILD)/kernel/idle.o \
$(BUILD)/kernel/mutex.o \
$(BUILD)/kernel/gate.o \
$(BUILD)/kernel/schedule.o \
Expand Down

0 comments on commit b7988b0

Please sign in to comment.