]> Zhao Yanbai Git Server - kernel.git/commitdiff
fix bug in semaphore
authoracevest <zhaoyanbai@126.com>
Sun, 22 Jun 2014 14:56:54 +0000 (22:56 +0800)
committeracevest <zhaoyanbai@126.com>
Wed, 3 Nov 2021 02:40:09 +0000 (10:40 +0800)
12 files changed:
drivers/console.c [new file with mode: 0644]
drivers/console.h [new file with mode: 0644]
drivers/keyboard.c
include/list.h
include/sched.h
include/semaphore.h
include/wait.h
kernel/sched.c
kernel/semaphore.c
kernel/wait.c
mm/kmalloc.c
setup/setup.c

diff --git a/drivers/console.c b/drivers/console.c
new file mode 100644 (file)
index 0000000..c393b73
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * ------------------------------------------------------------------------
+ *   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);
+}
diff --git a/drivers/console.h b/drivers/console.h
new file mode 100644 (file)
index 0000000..a18325d
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * ------------------------------------------------------------------------
+ *   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;
index c75b1819de80b05d12176dab0e022d3d4f1a807b..2c0f9f836236ce5e42a23e4a13a026a3d083cfb5 100644 (file)
 #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);
@@ -57,32 +45,21 @@ void    kbd_handler(unsigned int irq, pt_regs_t * regs, void *dev_id)
     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;
 }
index f57d998d32c0073f84f55e0d2abedb84f28b41e8..ddd7d51d0185d9bb4951b445cf1e22b06b2f0a6b 100644 (file)
@@ -49,6 +49,13 @@ do{                             \
         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;
index b6d104ad6c6313ca58397d83ce4f5eff8f835755..f5a87b189ca82f80fdb6a3b842223bf4518f255e 100644 (file)
@@ -24,7 +24,6 @@
 
 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;
index 50d76bec1d71d1f7fcc75c855426bae410135773..8ea8d783a5f27d6e7616113022055e9995ae82f3 100644 (file)
@@ -19,14 +19,14 @@ typedef struct semaphore
     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);
index 199bd3c513499453210bc7743b9545aade42a3f8..fd74d45a580b35137500c6b8f3f706099a83d5b8 100644 (file)
@@ -35,15 +35,16 @@ typedef struct
 #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);
index 1e6117664c504a8aad9294f63cc9d5092e608adf..59c485953097e8b611039255fac04cd173ff5ea0 100644 (file)
@@ -164,7 +164,8 @@ unsigned long schedule()
     task_union *prev = current;
     task_union *next = sel;
 
-    context_switch(prev, next);
+    if(prev != sel)
+        context_switch(prev, next);
 }
 
 void debug_sched()
@@ -172,14 +173,3 @@ 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)
-{
-
-}
index 8be4d053d828774c4b3f8af581ea1c83ce91819b..8f80703e6b201483ad0df9dd1bed3c14d74c6578 100644 (file)
@@ -33,7 +33,7 @@ void __down(semaphore_t *s)
     DECLARE_SEMAPHORE_WAITER(waiter, task);
     list_add_tail(&waiter.list, &s->wait_list);
 
-    while(true)
+    //while(true)
     {
         task->state = TASK_WAIT;
 
@@ -42,7 +42,7 @@ void __down(semaphore_t *s)
         disable_irq();
 
         if(waiter.up)
-            break;
+            ;//break;
     }
 }
 
@@ -69,6 +69,8 @@ void __up(semaphore_t *s)
     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;
 }
 
 
index 6f785760f22c655ac4b68ab525efc2dd82320070..b06b537388ea6e31749db01d3f302f10f9253647 100644 (file)
@@ -32,3 +32,16 @@ void del_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
     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.
+}
index 4de3411e3b4d6283f0cfff5940a8d0b8922e744d..b22ab1f27cd7e1bac2cfc4048e8f4425aaab9e83 100644 (file)
 #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);
 }
index 8c267665296afc7f2ca63e397e09d150b44585df..9adf6df0e905daf1447d3c62507169ddf2fb88e6 100644 (file)
@@ -33,6 +33,7 @@ extern void setup_fs();
 extern void setup_ext2();
 
 extern void reboot();
+extern void cnsl_init();
 
 #define HZ 10
 #define CLOCK_TICK_RATE 1193180
@@ -76,6 +77,9 @@ void setup_kernel()
     setup_sysc();
     setup_pci();
 
+    cnsl_init();
+
+
     setup_tasks();
 
     setup_irqs();