Repos / kernel.git / commitdiff
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 09f43a5)
raw | patch | inline | side by side (parent: 09f43a5)
| author | zhaoyanbai <zhaoyanbai@126.com> | |
| Tue, 23 May 2023 16:44:08 +0000 (00:44 +0800) | ||
| committer | zhaoyanbai <zhaoyanbai@126.com> | |
| Tue, 23 May 2023 16:44:08 +0000 (00:44 +0800) |
| Makefile | patch | blob | history | |
| boot/reboot.S | patch | blob | history | |
| include/task.h | patch | blob | history | |
| kernel/clock.c | patch | blob | history | |
| kernel/fork.c | patch | blob | history | |
| kernel/interrupts.S | patch | blob | history | |
| kernel/sched.c | patch | blob | history | |
| kernel/task_init.c | patch | blob | history | |
| kernel/task_root.c | patch | blob | history | |
| kernel/task_user.c | patch | blob | history |
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
%.c.o: %.c ${HEADER_FILES}
${CC} ${CFLAGS} $< -o $@
+.PHONY: c
c:
rm -f $(KERNELBIN)
+.PHONY: clean
clean:
rm -f $(OBJS)
rm -f $(KERNELBIN) $(SYSTEMMAP)
+.PHONY: install
install:
cp -p KERNEL.BIN /boot/
sync
mkdir -p /kernel/bin/
cp bin/hello /kernel/bin/
cp bin/shell /kernel/bin/
+
+.PHONY: cp
cp:
cd bin && make clean
cd bin && make
diff --git a/boot/reboot.S b/boot/reboot.S
--- a/boot/reboot.S
+++ b/boot/reboot.S
msg_reboot: .asciz "\nREBOOTING...........\n"
-msg_poweroff: .asciz "\nSHUTDOWN SYSTEM NOW......\n"
\ No newline at end of file
+msg_poweroff: .asciz "\nSHUTDOWN SYSTEM NOW......\n"
diff --git a/include/task.h b/include/task.h
--- a/include/task.h
+++ b/include/task.h
unsigned long esp;
unsigned long eip;
- long weight;
- long priority;
+ uint32_t ticks;
+ uint32_t priority;
+ uint32_t jiffies;
pid_t pid;
pid_t ppid;
#define ROOT_TSK_PID (0)
-#define TASK_INIT_WEIGHT 0
+// #define TASK_INIT_WEIGHT 0
#define get_tsk_from_list(p) list_entry((p), Task, list)
#define del_tsk_from_list(tsk) list_del((&tsk->list))
diff --git a/kernel/clock.c b/kernel/clock.c
--- a/kernel/clock.c
+++ b/kernel/clock.c
#include <system.h>
#include <wait.h>
-volatile unsigned int jiffies = 0;
+volatile uint32_t jiffies = 0;
unsigned int sys_clock() { return jiffies; }
void clk_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) {
- jiffies++;
-
if (jiffies % 100 == 0) {
printl(MPL_CLOCK, "clock irq: %d", jiffies);
}
unsigned long iflags;
irq_save(iflags);
+ jiffies++;
+
+ current->jiffies = jiffies;
+ current->ticks--;
+
task_union *p = 0;
list_head_t *t = 0;
list_head_t *pos = 0;
diff --git a/kernel/fork.c b/kernel/fork.c
--- a/kernel/fork.c
+++ b/kernel/fork.c
tsk->pid = get_next_pid();
tsk->ppid = current->pid;
+ tsk->priority = current->priority;
+ tsk->ticks = tsk->priority;
pt_regs_t *child_regs = ((pt_regs_t *)(TASK_SIZE + (unsigned long)tsk)) - 1;
printk("tsk %08x child_regs esp %08x esp0 %08x\n", tsk, tsk->esp, tsk->esp0);
tsk->state = TASK_INITING;
- tsk->weight = TASK_INIT_WEIGHT;
INIT_LIST_HEAD(&tsk->list);
unsigned long iflags;
diff --git a/kernel/interrupts.S b/kernel/interrupts.S
--- a/kernel/interrupts.S
+++ b/kernel/interrupts.S
.global _irq_handler
.extern irq_handler
.extern schedule
-.extern irq_reenter
+.extern try_to_reschedule
_irq_handler:
SAVE_REGS
movl %esp, %eax
call irq_handler
- cmpl $0, (irq_reenter)
- jnz restore_regs
- call schedule
+ call try_to_reschedule
restore_regs:
RESTORE_REGS
diff --git a/kernel/sched.c b/kernel/sched.c
--- a/kernel/sched.c
+++ b/kernel/sched.c
root_task.pid = get_next_pid();
root_task.ppid = 0;
root_task.state = TASK_READY;
- root_task.weight = TASK_INIT_WEIGHT;
- root_task.priority = 100;
+ root_task.priority = 7;
+ root_task.ticks = root_task.priority;
strcpy(root_task.name, "root");
list_add(&root_task.list, &all_tasks);
extern uint32_t disk_inter_cnt;
unsigned long schedule() {
- task_union *sel = &root_task;
+ task_union *root = &root_task;
+ task_union *sel = 0;
task_union *p = 0;
list_head_t *pos = 0, *t = 0;
printl(MPL_X, "disk req %u consumed %u irq %u", disk_request_cnt, disk_handled_cnt, disk_inter_cnt);
#if 1
- bool need_reset_weight = true;
list_for_each_safe(pos, t, &all_tasks) {
p = list_entry(pos, task_union, list);
-
if (p == &root_task) {
continue;
}
-
if (p->state != TASK_READY) {
continue;
}
- if (p->weight < p->priority) {
- need_reset_weight = false;
- break;
- }
- }
-
- if (need_reset_weight) {
- list_for_each_safe(pos, t, &all_tasks) {
- p = list_entry(pos, task_union, list);
- if (p->state != TASK_READY) {
- continue;
+ if (sel == 0) {
+ sel = p;
+ } else if (sel->jiffies >= p->jiffies) {
+ uint32_t delta = sel->jiffies - p->jiffies;
+ if (delta > 3 * p->ticks) {
+ sel = p;
}
- p->weight = 0;
}
}
- list_for_each_safe(pos, t, &all_tasks) {
- p = list_entry(pos, task_union, list);
- if (p == &root_task) {
- continue;
- }
- if (p->state != TASK_READY) {
- continue;
- }
+ sel = sel != 0 ? sel : root;
- if (p->weight > p->priority) {
- continue;
- }
+ irq_restore(iflags);
+ sel->sched_cnt++;
+ // printk("%08x %s ticks %d state: %s\n", sel, sel->name, sel->ticks, task_state(sel->state));
+ task_union *prev = current;
+ task_union *next = sel;
- if (p->weight < sel->weight) {
- sel = p;
+ if (prev != next) {
+ // printk("switch to: %s:%d\n", next->name, next->pid);
+ list_for_each_safe(pos, t, &all_tasks) {
+ p = list_entry(pos, task_union, list);
+ printl(MPL_TASK_0 + p->pid, " "); // 清掉上一次显示的 '>'
+ printl(MPL_TASK_0 + p->pid, "%s%4s:%d [%08x] state %s ticks %03d %02d sched %u", next == p ? ">" : " ",
+ p->name, p->pid, p, task_state(p->state), p->ticks, p->priority, p->sched_cnt);
}
+ context_switch(prev, next);
}
#else
+ task_union *sel = &root_task;
+ task_union *p = 0;
+ list_head_t *pos = 0, *t = 0;
+
+ unsigned long iflags;
+ irq_save(iflags);
+
float min_ratio = 1.0;
bool need_reset_weight = true;
list_for_each_safe(pos, t, &all_tasks) {
min_ratio = ratio;
}
}
-#endif
irq_restore(iflags);
sel->sched_cnt++;
- sel->weight += 13;
- // printk("%08x %s weight %d state: %s\n", sel, sel->name, sel->weight, task_state(sel->state));
+ sel->ticks += 13;
+ // printk("%08x %s ticks %d state: %s\n", sel, sel->name, sel->ticks, task_state(sel->state));
task_union *prev = current;
task_union *next = sel;
list_for_each_safe(pos, t, &all_tasks) {
p = list_entry(pos, task_union, list);
printl(MPL_TASK_0 + p->pid, " "); // 清掉上一次显示的 '>'
- printl(MPL_TASK_0 + p->pid, "%s%4s:%d [%08x] state %s weight %03d %03d sched %u", next == p ? ">" : " ",
- p->name, p->pid, p, task_state(p->state), p->weight, p->priority, p->sched_cnt);
+ printl(MPL_TASK_0 + p->pid, "%s%4s:%d [%08x] state %s ticks %03d %03d sched %u", next == p ? ">" : " ",
+ p->name, p->pid, p, task_state(p->state), p->ticks, p->priority, p->sched_cnt);
}
context_switch(prev, next);
}
+#endif
+}
+
+// 必需在关中断的情况下调用
+void try_to_reschedule() {
+ if (irq_reenter != 0) {
+ return;
+ }
+
+ if (0 == current->ticks) {
+ current->ticks = current->priority;
+ schedule();
+ }
}
void debug_sched() {
diff --git a/kernel/task_init.c b/kernel/task_init.c
--- a/kernel/task_init.c
+++ b/kernel/task_init.c
#include <types.h>
void init_task_entry() {
- current->priority = 100;
+ current->priority = 10;
// 继续内核未完成的初始化
// 这些初始化在开中断的情况下完成
while (1) {
asm("hlt;");
+ sysc_wait(2);
}
}
diff --git a/kernel/task_root.c b/kernel/task_root.c
--- a/kernel/task_root.c
+++ b/kernel/task_root.c
u16 disk_buf2[256];
void taskA_entry() {
- current->priority = 99;
+ current->priority = 9;
while (1) {
sysc_wait(7);
}
void taskB_entry() {
- current->priority = 99;
+ current->priority = 9;
while (1) {
sysc_wait(10);
}
void taskC_entry() {
- current->priority = 99;
+ current->priority = 19;
while (1) {
sysc_wait(2);
kernel_task("init", init_task_entry);
kernel_task("disk", disk_task_entry);
- kernel_task("user", user_task_entry);
+ // kernel_task("user", user_task_entry);
// for (int i = 0; i < 100; i++) {
// asm("hlt;");
kernel_task("tskB", taskB_entry);
kernel_task("tskC", taskC_entry);
+ current->priority = 1;
while (1) {
asm("hlt;");
}
diff --git a/kernel/task_user.c b/kernel/task_user.c
--- a/kernel/task_user.c
+++ b/kernel/task_user.c
}
void user_task_entry() {
- current->priority = 90;
+ current->priority = 7;
unsigned long ring3_text_page = va2pa(ring3_entry);
unsigned long ring3_bss_page = va2pa(alloc_one_page(0));