From: acevest Date: Sat, 11 May 2024 04:39:00 +0000 (+0800) Subject: 处理ticks可能减为负的情况 X-Git-Url: http://zhaoyanbai.com/repos/?a=commitdiff_plain;h=c048943b34957ae7f4927f0b5e5316da039ea2ac;p=kernel.git 处理ticks可能减为负的情况 --- diff --git a/boot/boot.c b/boot/boot.c index f7ad9f0..7498796 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -53,7 +53,7 @@ void setup_gdt(); void setup_idt(); void setup_gates(); void set_tss(); -void setup_i8253(uint16_t); +void setup_i8254(uint16_t); void setup_boot_irqs(); void check_kernel(unsigned long addr, unsigned long magic) { @@ -78,7 +78,7 @@ void check_kernel(unsigned long addr, unsigned long magic) { setup_boot_irqs(); - setup_i8253(100); + setup_i8254(100); boot_delay(DEFAULT_BOOT_DELAY_TICKS); diff --git a/kernel/clock.c b/kernel/clock.c index f992557..5110a43 100644 --- a/kernel/clock.c +++ b/kernel/clock.c @@ -39,27 +39,36 @@ void clk_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) { current->jiffies = jiffies; // 中断目前虽然不能嵌套,但依然可以打断前一个中断的下半部分处理 - // 若前一个时钟中断将这个值减到0 - // 同时其下半部分处理时间过长,直到这个时钟中断还没处理完 - // 那么这个时钟中断是完全可以打断它,且在这里把这个ticks从0减到负数 - // 而这个是uint32_t型,因此会溢出成0xFFFFFFFF - // if (current->ticks > 0) { - // current->ticks--; - // } - if (0 == --current->ticks) { - current->need_resched = 1; + // 若前一个时钟中断将这个值减到0,会将该进程设置为need_resched,同时分配新的时间片值,以在下半部处理完后再重新调度就绪进程运行 + // 而如果其下半部分需要处理的事情很多,处理时间过长,两个时钟中断之间的时间还不足以处理完 + // 那么下一个时钟中断是完全可以打断还没处理完的下半部逻辑 + // 打断后该时钟中断不应该继续减少该进程的时间片,因为这会造成该进程在后续的调底中少了实际的运行时间 + + if (1 == current->need_resched) { + // 这种情况必然已经发生了该时钟中断打断了下半部处理程序 + return; } + current->ticks--; + +#if 1 int value = (int)(current->ticks); if (min_ticks_value > value) { min_ticks_value = value; min_ticks_name = current->name; } +#endif - printl(MPL_TEST, "%20s %10d %20s %10d", min_ticks_name, min_ticks_value, current->name, value); + if (0 == current->ticks) { + current->need_resched = 1; + current->ticks = current->priority; + current->turn++; + } + + assert(current->ticks <= TASK_MAX_PRIORITY); // 防止ticks被减到0后再减溢出 - // assert(current->ticks <= TASK_MAX_PRIORITY); // 防止ticks被减到0后再减溢出 + printl(MPL_TEST, "%20s %10d %20s %10d", min_ticks_name, min_ticks_value, current->name, value); add_irq_bh_handler(clk_bh_handler, NULL); } @@ -88,23 +97,56 @@ void clk_bh_handler(void *arg) { debug_print_all_tasks(); } -void setup_i8253(uint16_t hz) { - // 最低频率为18.2Hz(1193180/65536,最大计数值是65536是因为往计数器里写0就从65536开始计数) +uint16_t read_i8254_counter(uint8_t counter_no) { + const uint8_t i8254_cmd_port = 0x43; + const uint8_t i8254_data_port = 0x40 + counter_no; + + assert(counter_no < 3); + + uint8_t cmd = 0x00; + cmd |= 0x03 << 6; // read back command + cmd |= 0x01 << 4; // don't latch status + cmd |= 0x01 << (counter_no + 1); // read back timer channel n + + uint16_t value = 0; + + outb_p(cmd, i8254_cmd_port); + + value |= inb(i8254_data_port) << 0; + value |= inb(i8254_data_port) << 8; + + return value; +} + +void setup_i8254(uint16_t hz) { + // PC/AT 8254的连接方法 + // 第0个计数器连连到了8259A的IRQ0 + // 第1个计数器连连到了DRAM刷新电路,通常情况下,该通道的频率设置为15.2kHz + // 第2个计数器连连到了音频驱动单元,可以通过频率控制发声 + + // 8254的最低频率为18.2Hz(1193180/65536,最大计数值是65536是因为往计数器里写0就从65536开始计数) assert(hz >= 19); const uint8_t counter_no = 0; // 第0个计数器 const uint8_t read_write_latch = 3; // 0 锁存数据供CPU读;1只读写低字节;2只读写高字节;3先读写低字节,后读写高字节 const uint8_t mode = 2; // + const uint8_t BCD = 0; // 0 二进制;1 BCD码 - const uint8_t cmd = (counter_no << 6) | (read_write_latch << 4) | (mode << 1); // 第0位为0表示二进制,为1表示BCD + const uint8_t cmd = + ((counter_no & 0x03) << 6) | ((read_write_latch & 0x03) << 4) | ((mode & 0x07) << 1) | ((BCD & 0x01) << 0); - const uint8_t i8253_cmd_port = 0x43; - const uint8_t i8253_data_port = 0x40 + counter_no; + const uint8_t i8254_cmd_port = 0x43; + const uint8_t i8254_data_port = 0x40 + counter_no; const uint32_t clock_rate = 1193180; uint16_t latch = (clock_rate + hz / 2) / hz; - outb_p(cmd, i8253_cmd_port); - outb_p(latch & 0xFF, i8253_data_port); - outb(latch >> 8, i8253_data_port); + // 必须先写控制命令,再写入初始计数值 + outb_p(cmd, i8254_cmd_port); + outb_p((latch >> 0) & 0xFF, i8254_data_port); + outb_p((latch >> 8) & 0xFF, i8254_data_port); + + // for (uint8_t i = 0; i < 3; i++) { + // printk("i8254 counter%u value %u\n", i, read_i8254_counter(i)); + // } } diff --git a/kernel/sched.c b/kernel/sched.c index cc0d6d0..471cd2a 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -167,12 +167,12 @@ const char *task_state(unsigned int state) { void debug_print_all_tasks() { task_t *p = 0; list_head_t *pos = 0, *t = 0; - printl(MPL_TASK_TITLE, " NAME STATE TK/PI REASON SCHED KEEP"); + printl(MPL_TASK_TITLE, " NAME STATE TK/PI REASON SCHED KEEP TURN"); list_for_each_safe(pos, t, &all_tasks) { p = list_entry(pos, task_t, list); - printl(MPL_TASK_0 + p->pid, "%08x%s%-6s:%u %s %02u/%02u %-10s %-10u %-10u", p, + printl(MPL_TASK_0 + p->pid, "%08x%s%-6s:%u %s %02u/%02u %-10s %-10u %-10u %-10u", p, p->state == TASK_RUNNING ? ">" : " ", p->name, p->pid, task_state(p->state), p->ticks, p->priority, - p->reason, p->sched_cnt, p->sched_keep_cnt); + p->reason, p->sched_cnt, p->sched_keep_cnt, p->turn); } } @@ -188,11 +188,6 @@ void schedule() { unsigned long iflags; irq_save(iflags); - if (0 == current->ticks) { - current->turn++; - current->ticks = current->priority; - } - if (current->state == TASK_RUNNING) { current->state = TASK_READY; } @@ -233,10 +228,10 @@ void schedule() { task_t *prev = current; task_t *next = sel != 0 ? sel : root; + prev->need_resched = 0; + next->state = TASK_RUNNING; next->reason = ""; - next->ticks = 5; - prev->need_resched = 0; if (prev != next) { next->sched_cnt++; diff --git a/kernel/setup.c b/kernel/setup.c index 12e2314..a44c169 100644 --- a/kernel/setup.c +++ b/kernel/setup.c @@ -24,7 +24,7 @@ extern void init_buffer(); extern void setup_gdt(); extern void setup_idt(); extern void setup_gate(); -void setup_i8253(uint16_t hz); +void setup_i8254(uint16_t hz); extern void detect_cpu(); extern void setup_sysc(); extern void setup_pci(); @@ -87,7 +87,7 @@ void setup_kernel() { boot_delay(DEFAULT_BOOT_DELAY_TICKS); - setup_i8253(100); + setup_i8254(100); setup_irqs(); void ide_init();