#include <system.h>
#include <tty.h>
#include <vt.h>
+#include <irq.h>
void reboot();
void poweroff();
#include "io.h"
#include "console.h"
+#include <assert.h>
#define COMM1_PORT 0x3F8
#define COMM2_PORT 0x2F8
#include "vt.h"
#include <vga.h>
+#include <assert.h>
+#include <string.h>
static vc_t vcs[VC_COUNT];
static vc_t* fg_vc = &vcs[0];
+++ /dev/null
-/*
- * ------------------------------------------------------------------------
- * File Name: completion.h
- * Author: Zhao Yanbai
- * 2021-11-27 10:58:33 Saturday CST
- * Description: none
- * ------------------------------------------------------------------------
- */
-
-#pragma once
-
-#include <wait.h>
-
-typedef struct deprecated_completion {
- unsigned int done;
- wait_queue_head_t wait;
-
- // 仅用于调试
- char* name;
-} deprecated_completion_t;
-
-#define DEPRECATED_COMPLETION_INITIALIZER(x) {0, WAIT_QUEUE_HEAD_INITIALIZER(x.wait)}
-
-void init_deprecated_completion(deprecated_completion_t* x);
-
-void wait_deprecated_completion(deprecated_completion_t* x);
-
-// 一次只唤醒一个进程
-void complete(deprecated_completion_t* x);
#pragma once
-#include <wait.h>
+#include <types.h>
typedef struct console console_t;
void register_console(console_t* console);
int console_write(const char* buf, size_t size);
-
-#define CNSL_QUEUE_SIZE 1024
-
-typedef struct cnsl_queue {
- unsigned int head;
- unsigned int tail;
- wait_queue_head_t wait;
- char data[CNSL_QUEUE_SIZE];
-} cnsl_queue_t;
-
-typedef struct cnsl {
- cnsl_queue_t rd_q;
- cnsl_queue_t wr_q;
- cnsl_queue_t sc_q;
-} cnsl_t;
-
-int cnsl_kbd_write(char c);
#pragma once
#include <buffer.h>
-#include <completion.h>
#include <fs.h>
#include <list.h>
#include <sync.h>
+++ /dev/null
-/*
- * ------------------------------------------------------------------------
- * File Name: semaphore.h
- * Author: Zhao Yanbai
- * Sun Jun 22 13:53:18 2014
- * Description: none
- * ------------------------------------------------------------------------
- */
-
-#pragma once
-
-#include <list.h>
-
-typedef struct deprecated_semaphore {
- volatile unsigned int cnt;
- list_head_t wait_list;
-} deprecated_semaphore_t;
-
-#define SEMAPHORE_INITIALIZER(name, n) {.cnt = (n), .wait_list = LIST_HEAD_INIT((name).wait_list)}
-
-void deprecated_semaphore_init(deprecated_semaphore_t* s, unsigned int v);
-
-// down
-// 如果s->cnt > 0不会立即重新调度进程
-// 如果s->cnt == 0 会重新调度进程
-volatile void down(deprecated_semaphore_t* s);
-
-// up
-// 只会唤醒进程,但不会立即重新调度进程
-volatile void up(deprecated_semaphore_t* s);
-
-typedef deprecated_semaphore_t deprecated_mutex_t;
-
-#define MUTEX_INITIALIZER(name) {.cnt = (1), .wait_list = LIST_HEAD_INIT((name).wait_list)}
-
-#define DECLARE_MUTEX(name) mutex_t name = MUTEX_INITIALIZER(name)
-
-#define INIT_MUTEX(ptr) \
- do { \
- (ptr)->cnt = 1; \
- INIT_LIST_HEAD(&((ptr)->wait_list)); \
- } while (0)
-void deprecated_mutex_init(deprecated_mutex_t*);
-void deprecated_mutex_lock(deprecated_mutex_t*);
-void deprecated_mutex_unlock(deprecated_mutex_t*);
#include <types.h>
#include <sync.h>
-#include <semaphore.h>
#define TTY_MAX_NAME_LEN 32
#define TTY_MAX_COUNT 8
+++ /dev/null
-/*
- *--------------------------------------------------------------------------
- * File Name: wait.h
- *
- * Author: Zhao Yanbai [zhaoyanbai@126.com]
- * Mon Feb 22 20:50:56 2010
- *
- * Description: none
- *
- *--------------------------------------------------------------------------
- */
-
-#pragma once
-
-#include <irq.h>
-#include <list.h>
-#include <sched.h>
-
-typedef struct wait_queue_head {
- list_head_t task_list;
-} wait_queue_head_t;
-
-typedef struct {
- task_t* task;
- list_head_t entry;
-} wait_queue_entry_t;
-
-#define WAIT_QUEUE_HEAD_INITIALIZER(name) {.task_list = LIST_HEAD_INIT((name).task_list)}
-
-#define DECLARE_WAIT_QUEUE_HEAD(name) wait_queue_head_t name = WAIT_QUEUE_HEAD_INITIALIZER(name)
-
-#define WAIT_QUEUE_ENTRY_INITIALIZER(name, tsk) {.task = tsk, .entry = LIST_HEAD_INIT((name).entry)}
-
-#define DECLARE_WAIT_QUEUE_ENTRY(name, tsk) wait_queue_entry_t name = WAIT_QUEUE_ENTRY_INITIALIZER(name, tsk)
-
-void init_wait_queue_head(wait_queue_head_t* wqh);
-void add_wait_queue(wait_queue_head_t* head, wait_queue_entry_t* wq);
-void del_wait_queue(wait_queue_head_t* head, wait_queue_entry_t* wq);
-
-// prepare_to_wait 不会调用schedule
-void __prepare_to_wait(wait_queue_head_t* head, wait_queue_entry_t* wqe, unsigned int state);
-void prepare_to_wait(wait_queue_head_t* head, wait_queue_entry_t* wq, unsigned int state);
-
-void __end_wait(wait_queue_entry_t* wq);
-
-// 使用这个函数的时候需要注意
-// 设置condition为真的语句和wake_up原子地执行
-// wake_up只唤醒不立即重新调度
-void wake_up(wait_queue_head_t* head);
-
-void wake_up_all(wait_queue_head_t* head);
-
-// nr == 0 代表唤醒所有
-void __wake_up(wait_queue_head_t* head, int nr);
-
-// 以下wait_event被定义成宏,而不是函数是因为condition
-// condition可能是一个表达式,如果定义成函数,往下传递就直接变成了一个值
-// 如果在某一步这个值变了,并不会反应在这些逻辑判断上
-#define __wait_event(head, condition) \
- do { \
- DECLARE_WAIT_QUEUE_ENTRY(__wait, current); \
- unsigned long flags; \
- while (1) { \
- irq_save(flags); \
- __prepare_to_wait(head, &__wait, TASK_WAIT); \
- if ((condition)) { \
- __end_wait(&__wait); \
- irq_restore(flags); \
- break; \
- } else { \
- irq_restore(flags); \
- void schedule(); \
- schedule(); \
- irq_save(flags); \
- __end_wait(&__wait); \
- irq_restore(flags); \
- } \
- } \
- } while (0)
-
-#define deprecated_wait_event(head, condition) \
- do { \
- if ((condition)) { \
- break; \
- } \
- __wait_event(head, (condition)); \
- } while (0)
#include <printk.h>
#include <sched.h>
#include <system.h>
-#include <wait.h>
volatile uint64_t jiffies = 0; // TODO uint64: undefined reference to `__umoddi3'
+++ /dev/null
-/*
- * ------------------------------------------------------------------------
- * File Name: completion.c
- * Author: Zhao Yanbai
- * 2021-11-27 10:58:46 Saturday CST
- * Description: none
- * ------------------------------------------------------------------------
- */
-
-#include <completion.h>
-#include <sched.h>
-
-void wait_deprecated_completion(deprecated_completion_t* x) {
- uint32_t eflags;
- DECLARE_WAIT_QUEUE_ENTRY(__wait, current);
-
- while (1) {
- irq_save(eflags);
- if (x->done > 0) {
- x->done--;
- irq_restore(eflags);
- return;
- }
-
- __prepare_to_wait(&x->wait, &__wait, TASK_WAIT);
- irq_restore(eflags);
-
- schedule();
-
- irq_save(eflags);
- __end_wait(&__wait);
- irq_restore(eflags);
- }
-}
-
-void complete(deprecated_completion_t* x) {
- uint32_t iflags;
- irq_save(iflags);
- x->done++;
- wake_up_all(&x->wait);
- irq_restore(iflags);
-}
-
-void init_deprecated_completion(deprecated_completion_t* x) {
- x->done = 0;
- init_wait_queue_head(&x->wait);
-
- // 测试字段
- x->name = 0;
-}
#include <console.h>
#include <sched.h>
-#include <semaphore.h>
#include <string.h>
#include <tty.h>
-#include <wait.h>
#define MAX_CONSOLE_CNT 16
#include <sched.h>
#include <system.h>
-#include <wait.h>
int sysc_exit(int status) {
unsigned long flags;
#include "sched.h"
-#include <wait.h>
-
#include "assert.h"
#include "linkage.h"
#include "mm.h"
+++ /dev/null
-/*
- * ------------------------------------------------------------------------
- * File Name: semaphore.c
- * Author: Zhao Yanbai
- * Sun Jun 22 13:57:18 2014
- * Description: none
- * ------------------------------------------------------------------------
- */
-#include <irq.h>
-#include <sched.h>
-#include <semaphore.h>
-#include <task.h>
-
-typedef struct semaphore_waiter {
- list_head_t list;
- task_t* task;
-
- // 在Linux内核中这个结构体有一个up字段,这个字段的作用是防止进程被错误地唤醒
- // 例如
- // 如果一个进程在等待信号量时收到了一个信号(signal),它可能会被内核唤醒以处理这个信号。
- // 但是,如果这个进程没有真正获得信号量(即up字段为false)它应该继续等待信号量而不是继续执行
- // 本内核暂时还用不到这个字段
-
-} semaphore_waiter_t;
-
-void deprecated_semaphore_init(deprecated_semaphore_t* s, unsigned int v) {
- s->cnt = v;
- INIT_LIST_HEAD(&(s->wait_list));
-}
-
-volatile void down(deprecated_semaphore_t* s) {
- unsigned long iflags;
- irq_save(iflags);
-
- if (likely(s->cnt > 0)) {
- s->cnt--;
- irq_restore(iflags);
- } else {
- task_t* task = current;
- semaphore_waiter_t waiter;
- waiter.task = task;
- INIT_LIST_HEAD(&waiter.list);
- list_add(&waiter.list, &s->wait_list);
-
- // task->state = TASK_WAIT;
- task_set_wait(task);
- task->reason = "down";
-
- irq_restore(iflags);
-
- schedule();
- }
-}
-
-volatile void up(deprecated_semaphore_t* s) {
- unsigned long iflags;
- irq_save(iflags);
-
- if (list_empty(&s->wait_list)) {
- s->cnt++;
- irq_restore(iflags);
- } else {
- semaphore_waiter_t* waiter = list_first_entry(&s->wait_list, semaphore_waiter_t, list);
- assert(waiter != 0);
- list_del(&waiter->list);
- task_t* task = waiter->task;
-
- // task->state = TASK_READY;
- task_set_ready(task);
- task->reason = "up";
-
- irq_restore(iflags);
-
- // 之前把这里的schedule()删除了,是因为up()被用到了 ide_irq_bh_handler 里去唤醒磁盘任务
- // 但在中断的底半处理,不应该切换任务,因为会引起irq里的reenter问题,导致不能再进底半处理,也无法切换任务
- // 但这种方法是不对的
- // 后来就改用完成量来通知磁盘任务
- // 因此信号量这里就可以加回schedule()了
- schedule();
- }
-}
-
-void deprecated_mutex_init(deprecated_mutex_t* s) {
- INIT_MUTEX(s);
-}
-void deprecated_mutex_lock(deprecated_mutex_t* s) {
- down(s);
-}
-void deprecated_mutex_unlock(deprecated_mutex_t* s) {
- up(s);
-}
+++ /dev/null
-/*
- *--------------------------------------------------------------------------
- * File Name: wait.c
- *
- * Author: Zhao Yanbai [zhaoyanbai@126.com]
- * Mon Feb 22 20:45:22 2010
- *
- * Description: none
- *
- *--------------------------------------------------------------------------
- */
-#include <sched.h>
-#include <wait.h>
-
-volatile void init_wait_queue_head(wait_queue_head_t* wqh) {
- INIT_LIST_HEAD(&(wqh->task_list));
-}
-
-volatile void prepare_to_wait(wait_queue_head_t* head, wait_queue_entry_t* wqe, unsigned int state) {
- unsigned long flags;
- irq_save(flags);
- __prepare_to_wait(head, wqe, state);
- irq_restore(flags);
-}
-
-volatile void __prepare_to_wait(wait_queue_head_t* head, wait_queue_entry_t* wqe, unsigned int state) {
- // 进程可能等待一个condition满足后被唤醒
- // 然后又发现这个conditon又不满足了,需要继续wait
- // 这时候又会把自己加到等待队列里
- // 所以这里需要加一个判断,如果已经加过了,就不需要再加了,不然会出问题
- if (list_empty(&wqe->entry)) {
- // list_add_tail(&wqe->entry, &head->task_list);
- list_add(&wqe->entry, &head->task_list);
- }
-
- assert(state == TASK_WAIT);
- task_set_wait(current);
- current->reason = "p_wait";
-}
-
-volatile void __end_wait(wait_queue_entry_t* wqe) {
- task_set_ready(current);
- current->reason = "e_wait";
- // unsigned long flags;
- // irq_save(flags);
- list_del_init(&wqe->entry);
- // irq_restore(flags);
-}
-
-volatile void add_wait_queue(wait_queue_head_t* head, wait_queue_entry_t* wqe) {
- unsigned long flags;
- irq_save(flags);
- list_add_tail(&wqe->entry, &head->task_list);
- irq_restore(flags);
-}
-
-volatile void del_wait_queue(wait_queue_head_t* head, wait_queue_entry_t* wqe) {
- unsigned long flags;
- irq_save(flags);
- list_del(&wqe->entry);
- irq_restore(flags);
-}
-
-volatile void __wake_up(wait_queue_head_t* head, int nr) {
- unsigned long flags;
- wait_queue_entry_t *p, *tmp;
- irq_save(flags);
- list_for_each_entry_safe(p, tmp, &head->task_list, entry) {
- assert(p->task != NULL);
- // printk("wakeup %s\n", p->task->name);
- // p->task->state = TASK_READY;
- task_set_ready(p->task);
- p->task->reason = "wake_up";
-
- list_del_init(&p->entry);
-
- --nr;
- if (nr == 0) {
- break;
- }
- }
- irq_restore(flags);
-
- // no schedule() here.
-}
-
-volatile void wake_up(wait_queue_head_t* head) {
- //
- __wake_up(head, 1); // wake up one
-}
-
-volatile void wake_up_all(wait_queue_head_t* head) {
- //
- __wake_up(head, 0); // wake up all
-}