--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * File Name: console.c
+ * Author: Zhao Yanbai
+ * Sun Jun 22 18:50:13 2014
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+
+#include<string.h>
+#include<console.h>
+
+cnsl_queue_t cnsl_rd_q;
+cnsl_queue_t cnsl_wr_q;
+cnsl_queue_t cnsl_sc_q;
+
+static void cnsl_queue_init(cnsl_queue_t *cq)
+{
+ memset((void *)cq, 0, sizeof(*cq));
+
+ cq->head = 0;
+ cq->tail = 0;
+ init_wait_queue(&cq->wait);
+ //cq->data = kmalloc(CNSL_QUEUE_SIZE, 0);
+
+ printk("console queue data addr %08x\n", cq->data);
+}
+
+void cnsl_init()
+{
+ cnsl_queue_init(&cnsl_rd_q);
+ cnsl_queue_init(&cnsl_wr_q);
+ cnsl_queue_init(&cnsl_sc_q);
+}
--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * File Name: console.h
+ * Author: Zhao Yanbai
+ * Sun Jun 22 22:06:53 2014
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+
+#pragma once
+
+#include<wait.h>
+
+#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;
+
+extern cnsl_queue_t cnsl_rd_q;
+extern cnsl_queue_t cnsl_wr_q;
+extern cnsl_queue_t cnsl_sc_q;
#include <syscall.h>
#include <stdio.h>
#include <io.h>
-
-#define KBD_BUF_SIZE 256
-static struct
-{
- unsigned int count;
- unsigned int head,tail;
- unsigned char buf[KBD_BUF_SIZE];
-} kbd_buf;
-#define count kbd_buf.count
-#define head kbd_buf.head
-#define tail kbd_buf.tail
-#define buf kbd_buf.buf
-
-
+#include <console.h>
void reboot();
void poweroff();
void ide_debug();
void ide_status();
void debug_sched();
-void kbd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id)
+
+void kbd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id)
{
unsigned char scan_code;
scan_code = inb(0x60);
if(scan_code == 0x14) // t
debug_sched();
-// if(scan_code
-
- if(count < KBD_BUF_SIZE)
- {
- count++;
- buf[tail++] = scan_code;
- tail %= KBD_BUF_SIZE;
- }
-}
+ if((cnsl_rd_q.head+1) == cnsl_rd_q.tail)
+ goto end;
-inline int getscan_code()
-{
- unsigned int scan_code;
-
- //while(count <= 0);
- if(count <= 0) return -1;
+ cnsl_rd_q.data[cnsl_rd_q.head++] = (char) scan_code;
- scan_code = buf[head++];
- head %= KBD_BUF_SIZE;
- count--; //很明显这是临界资源但现在只能这样了
+end:
- return (0xFF & scan_code);
+ wake_up(&cnsl_rd_q.wait);
}
-
-int sysc_read_kbd()
+int sysc_read_kbd()
{
- return getscan_code();
+ DECLARE_WAIT_QUEUE(wait, current);
+ add_wait_queue(&cnsl_rd_q.wait, &wait);
+
+
+ return 0;
}
pos != (head); \
pos = tmp, tmp = pos->next)
+#define list_for_each_entry_safe(pos, tmp, head, member) \
+ for(pos=list_entry((head)->next, typeof(*pos), member), \
+ tmp=list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos=tmp, tmp=list_entry(tmp->member.next, typeof(*tmp), member))
+
+
static inline void _list_add(list_head_t *pnew, list_head_t *prev, list_head_t *next)
{
next->prev = pnew;
unsigned long schedule();
-inline void wake_up(wait_queue_t * wq);
-inline void sleep_on(wait_queue_t * wq);
+void wake_up(wait_queue_head_t *wqh);
extern task_union root_task;
list_head_t wait_list;
} semaphore_t;
-#define SEMAPHORE_INIT(name, n) \
+#define SEMAPHORE_INITIALIZER(name, n) \
{ \
.cnt = (n), \
.wait_list = LIST_HEAD_INIT((name).wait_list) \
}
#define DECLARE_MUTEX(name) \
- semaphore_t name = SEMAPHORE_INIT(name, 1)
+ semaphore_t name = SEMAPHORE_INITIALIZER(name, 1)
void down(semaphore_t *s);
void up(semaphore_t *s);
#define DECLARE_WAIT_QUEUE_HEAD(name) \
wait_queue_head_t name = WAIT_QUEUE_HEAD_INITIALIZER(name)
-#define WAIT_QUEUE_INITIALIZER(name) \
+#define WAIT_QUEUE_INITIALIZER(name, tsk) \
{ \
.task = tsk, \
.task_list = LIST_HEAD_INIT((name).task_list) \
}
-#define DECLARE_WAIT_QUEUE(name) \
- wait_queue_t name = WAIT_QUEUE_INITIALIZER(name)
+#define DECLARE_WAIT_QUEUE(name, tsk) \
+ wait_queue_t name = WAIT_QUEUE_INITIALIZER(name, tsk)
void init_wait_queue(wait_queue_head_t * wqh);
-
+void add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq);
+void del_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq);
task_union *prev = current;
task_union *next = sel;
- context_switch(prev, next);
+ if(prev != sel)
+ context_switch(prev, next);
}
void debug_sched()
task_union *p = list_entry(current->list.next, task_union, list);
p->state = (p->state == TASK_RUNNING) ? TASK_WAIT: TASK_RUNNING;
}
-
-
-inline void wake_up(wait_queue_t * wq)
-{
-
-}
-
-inline void sleep_on(wait_queue_t * wq)
-{
-
-}
DECLARE_SEMAPHORE_WAITER(waiter, task);
list_add_tail(&waiter.list, &s->wait_list);
- while(true)
+ //while(true)
{
task->state = TASK_WAIT;
disable_irq();
if(waiter.up)
- break;
+ ;//break;
}
}
semaphore_waiter_t *waiter = list_first_entry(&s->wait_list, semaphore_waiter_t, list);
list_del(&waiter->list);
waiter->up = 1;
+
+ waiter->task->state = TASK_RUNNING;
}
irq_restore(iflags);
}
+void wake_up(wait_queue_head_t *wqh)
+{
+ unsigned long flags;
+ wait_queue_t *p, *tmp;
+ irq_save(flags);
+ list_for_each_entry_safe(p, tmp, &wqh->task_list, task_list)
+ {
+ p->task->state = TASK_RUNNING;
+ }
+ irq_restore(flags);
+
+ // no schedule() here.
+}
#include <assert.h>
#include <system.h>
-static get_order(size_t size)
+static int get_order(size_t size)
{
- //printk("size:%08x ", size);
- size = ALIGN(size, PAGE_SIZE);
- //printk(" %08x\n", size);
int i;
- int n = size>>PAGE_SHIFT;
- for(i=0; i<MAX_OLD_ORDER; i++)
- {
- if(n<=(1UL<<i))
- break;
- }
-
- //printk("i:%d\n", i);
return i;
}
void *kmalloc_old(size_t size)
{
- assert(0<size && size<=32*PAGE_SIZE);
- int order = get_order(size);
void *p;
- pPage page = 0; //old_alloc_pages(order);
-
- if(page == NULL)
- return NULL;
-
- //printk("kmalloc_old:%08x %08x ", page->mapNR, page->mapNR<<PAGE_SHIFT);
- p = (void*)pa2va(page->mapNR<<PAGE_SHIFT);
-
- //printk("kmalloc_old: %08x\n", p);
-
return p;
}
void kfree_old(void *p)
{
- assert(p != NULL);
- pPage page = system.page_map;
- //printk("kfree_old:%08x %08x %08x ", p, va2pa(p));
- page += ((unsigned long)va2pa(p)>>PAGE_SHIFT);
- //printk("%08x\n", page->mapNR);
- //old_free_pages(page);
}
extern void setup_ext2();
extern void reboot();
+extern void cnsl_init();
#define HZ 10
#define CLOCK_TICK_RATE 1193180
setup_sysc();
setup_pci();
+ cnsl_init();
+
+
setup_tasks();
setup_irqs();