]> Zhao Yanbai Git Server - kernel.git/commitdiff
add semaphore
authoracevest <root@ace.laptop>
Sun, 22 Jun 2014 07:35:14 +0000 (15:35 +0800)
committeracevest <root@ace.laptop>
Sun, 22 Jun 2014 07:35:14 +0000 (15:35 +0800)
17 files changed:
.gitignore
drivers/ahci.c [deleted file]
drivers/ide.c
include/atomic.h [new file with mode: 0644]
include/fs.h
include/list.h
include/sched.h
include/semaphore.h [new file with mode: 0644]
include/task.h
include/wait.h
kernel/fork.c
kernel/init.c
kernel/sched.c
kernel/semaphore.c [new file with mode: 0644]
kernel/test_task.c [deleted file]
kernel/wait.c
setup/setup.c

index 8953fc460677a6391736260f5a8caf38d988e3b0..27fa5fc559ef25969a1132dcbe6b6af67326a5f1 100644 (file)
@@ -23,6 +23,7 @@
 *.BIN
 *.map
 *.diff
+*.swp
 bin/hw
 bin/sh
 a.*
diff --git a/drivers/ahci.c b/drivers/ahci.c
deleted file mode 100644 (file)
index fd112c9..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * ------------------------------------------------------------------------
- *   File Name: ahci.c
- *      Author: Zhao Yanbai
- *              Sat May 24 20:05:49 2014
- * Description: none
- * ------------------------------------------------------------------------
- */
-
-#include <types.h>
-#include <printk.h>
-#include <assert.h>
-#include <io.h>
-#include <irq.h>
-#include <pci.h>
-#include <system.h>
-
-
-ahci_init()
-{
-
-
-
-}
index 2d3bca39c8f4f280498981e6410a59ea48fc8ebc..f5556dfafdc26a2052e5d1c20f1ea41037c1617f 100644 (file)
@@ -15,6 +15,7 @@
 #include <irq.h>
 #include <pci.h>
 #include <system.h>
+#include <semaphore.h>
 
 unsigned int HD_CHL0_CMD_BASE = 0x1F0;
 unsigned int HD_CHL1_CMD_BASE = 0x170;
@@ -101,6 +102,8 @@ void ide_status()
     printk(" ide status %02x pci status %02x\n", idest, pcist);
 
 }
+
+
 void ide_debug()
 {
     u32    device;
@@ -116,6 +119,13 @@ void ide_debug()
     printk("ide_debug\n");
 }
 
+DECLARE_MUTEX(mutex);
+void debug_sem()
+{
+    down(&mutex);
+    ide_debug();
+}
+
 void init_pci_controller(unsigned int vendor, unsigned int device)
 {
     pci_device_t *pci = pci_find_device(vendor, device);
@@ -147,10 +157,11 @@ void ide_irq()
     insl(REG_DATA(0), buf, 512>>2);
     u16_t *s = (u16_t *) (buf+510);
     printk("hard disk data %04x\n", *s);
+    up(&mutex);
 }
 
 
-void    print_ide_identify(const char *buf)
+void print_ide_identify(const char *buf)
 {
     char    *p;
     short    *ident;
diff --git a/include/atomic.h b/include/atomic.h
new file mode 100644 (file)
index 0000000..f23f7f3
--- /dev/null
@@ -0,0 +1,16 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: atomic.h
+ *      Author: Zhao Yanbai
+ *              Sat Jun 21 18:37:21 2014
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+
+#pragma once
+
+#define atomic_inc(x) __sync_add_and_fetch((x),1)  
+#define atomic_dec(x) __sync_sub_and_fetch((x),1)  
+#define atomic_add(x,y) __sync_add_and_fetch((x),(y))  
+#define atomic_sub(x,y) __sync_sub_and_fetch((x),(y))
+
index 0afecd17e05b902584e6131206b845210fb0b07f..b55d3078113d7c91c4786e06bc1602d11a180ad0 100644 (file)
@@ -10,7 +10,7 @@
  *--------------------------------------------------------------------------
  */
 
-#ifndef    _FS_H
+#ifndef _FS_H
 #define _FS_H
 
 #include <types.h>
index 0e6145daafc65de87c21c57adb499a2eab4664ba..f57d998d32c0073f84f55e0d2abedb84f28b41e8 100644 (file)
@@ -37,6 +37,10 @@ do{                             \
 #define list_entry(ptr, type, member)       \
     ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
 
+#define list_first_entry(ptr, type, member) \
+    list_entry((ptr)->next, type, member)
+
+
 #define list_for_each(pos, head)            \
     for(pos = (head)->next; pos != (head); pos = pos->next)
 
@@ -85,3 +89,4 @@ static inline int list_empty(list_head_t *head)
 {
     return head->next == head;
 }
+
index 4073ba259370feb12f3cc100437d8ff50967ac00..b6d104ad6c6313ca58397d83ce4f5eff8f835755 100644 (file)
  *--------------------------------------------------------------------------
  */
 
-#ifndef    _SCHED_H
-#define _SCHED_H
+#pragma once
 
 #include <task.h>
-#define NR_TASKS    3
-//task_union *    tTasks[NR_TASKS];
-//void    add_task();
-//void    SetupTasks();
-//void    test_taskA();
-//void    test_taskB();
-//unsigned long schedule(pt_regs_t *    regs);
-unsigned long schedule();
+#include <wait.h>
 
-pid_t    get_next_pid();
-void    init_tsk_cr3(task_union *);
+#define FORK_USER 0
+#define FORK_KRNL 1
 
+unsigned long schedule();
 
 inline void wake_up(wait_queue_t * wq);
 inline void sleep_on(wait_queue_t * wq);
 
-#define TASK_CNT 64
-
 extern task_union root_task;
-
-#define FORK_KRNL 1
-
-#endif //_SCHED_H
diff --git a/include/semaphore.h b/include/semaphore.h
new file mode 100644 (file)
index 0000000..50d76be
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: semaphore.h
+ *      Author: Zhao Yanbai
+ *              Sun Jun 22 13:53:18 2014
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+
+#pragma once
+
+#include <list.h>
+#include <task.h>
+#include <irq.h>
+
+typedef struct semaphore
+{
+    volatile unsigned int cnt;
+    list_head_t wait_list;
+} semaphore_t;
+
+#define SEMAPHORE_INIT(name, n)                         \
+{                                                       \
+    .cnt        = (n),                                  \
+    .wait_list  = LIST_HEAD_INIT((name).wait_list)      \
+}
+
+#define DECLARE_MUTEX(name)                             \
+    semaphore_t name = SEMAPHORE_INIT(name, 1)
+
+void down(semaphore_t *s);
+void up(semaphore_t *s);
index a59e4bd0ccb729692f6465e984c4bdd2524d3a87..0ab883af25cba62f68f8ce2b7b4504f2d7ef7f67 100644 (file)
 #include <types.h>
 #include <processor.h>
 #include <system.h>
-#include <wait.h>
 #include <fs.h>
 
 enum
 {
     TASK_UNUSED,
     TASK_RUNNING,
-    TASK_UNINTERRUPTIBLE,
-    TASK_INTERRUPTIBLE,
+    TASK_WAIT,
+    //TASK_UNINTERRUPTIBLE,
+    //TASK_INTERRUPTIBLE,
     TASK_EXITING
 };
 
@@ -41,7 +41,6 @@ typedef union task_union
     {
         unsigned long   preempt_cnt;
 
-
         unsigned long    esp0;    /* kernel stack */
 
         /* for context switch */
@@ -60,8 +59,6 @@ typedef union task_union
 
         list_head_t list;
 
-        wait_queue_t    wait;
-
         pFile        fps[NR_OPENS];
 
     };
@@ -71,7 +68,6 @@ typedef union task_union
 
 task_union *alloc_task_union();
 
-
 static inline task_union *get_current()
 {
     task_union *tsk;
@@ -85,9 +81,6 @@ static inline task_union *get_current()
 
 #define TASK_INIT_WEIGHT 10
 
-extern    ListHead    tsk_list;
-
-#define add_tsk2list(tsk)    list_add_tail((&(tsk)->list), &tsk_list)
 #define get_tsk_from_list(p)    list_entry((p), Task, list)
 #define del_tsk_from_list(tsk)    list_del((&tsk->list))
 #endif
index a7b32cd34554cafa5e5eecd063764b3a9db23c15..199bd3c513499453210bc7743b9545aade42a3f8 100644 (file)
 #pragma once
 
 #include <list.h>
+#include <task.h>
+#include <irq.h>
 
 typedef struct
 {
-    list_head_t wait;
-
+    list_head_t task_list;
 } wait_queue_head_t;
 
-typedef list_head_t wait_queue_t;
+typedef struct
+{
+    task_union *task;
+    list_head_t task_list;
+} wait_queue_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_INITIALIZER(name)                \
+{                                                   \
+    .task       = tsk,                              \
+    .task_list  = LIST_HEAD_INIT((name).task_list)  \
+}
+
+#define DECLARE_WAIT_QUEUE(name)                    \
+    wait_queue_t name = WAIT_QUEUE_INITIALIZER(name)
+
 
 void init_wait_queue(wait_queue_head_t * wqh);
+
index d593a60836f3b673e20247fbe3f69f158bbdcc13..fb40c6bcd97b2bdf8af95918f8f0dd7796ba2005 100644 (file)
@@ -19,6 +19,7 @@ int sysc_fork(pt_regs_t regs)
 
 extern void ret_from_fork_user();
 extern void ret_from_fork_krnl();
+extern pid_t get_next_pid();
 
 int do_fork(pt_regs_t *regs, unsigned long flags)
 {
index eca95200f4109a8fecb56297fd037b21b3658262..7711f45767aa3013ccaf882da3ec2ec71803fbfe 100644 (file)
@@ -21,13 +21,22 @@ Desc    gdt[NGDT];
 
 char __initdata kernel_init_stack[KRNL_INIT_STACK_SIZE] __attribute__ ((__aligned__(PAGE_SIZE)));
 
-static unsigned int eid = 0;
+static unsigned int eid = 1;
+void debug_sem();
 void init_task_entry()
 {
     printk("hahahha %s\n", __func__);
     unsigned int id = eid++;
+    int i = 0;
     while(1)
     {
+        i++;
+        if(i == id*100)
+        {
+            printk("---READ---%d\n", id);
+            debug_sem();
+            printk("---END----%d\n", id);
+        }
         printk("%d", id);
         asm("sti;hlt;");
     }
index ddf5c40c5365525239da25f99a1c631c2ceb7a10..1e6117664c504a8aad9294f63cc9d5092e608adf 100644 (file)
@@ -91,15 +91,6 @@ task_union *alloc_task_union()
 }
 
 
-task_union *get_unused_task_pcb()
-{
-    unsigned int i;
-    for(i=0; i<TASK_CNT; ++i)
-    {
-
-    }
-}
-
 inline task_union *get_next_tsk()
 {
     return 0;
@@ -179,7 +170,7 @@ unsigned long schedule()
 void debug_sched()
 {
     task_union *p = list_entry(current->list.next, task_union, list);
-    p->state = (p->state == TASK_RUNNING) ? TASK_INTERRUPTIBLE : TASK_RUNNING;
+    p->state = (p->state == TASK_RUNNING) ? TASK_WAIT: TASK_RUNNING;
 }
 
 
diff --git a/kernel/semaphore.c b/kernel/semaphore.c
new file mode 100644 (file)
index 0000000..8be4d05
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * ------------------------------------------------------------------------
+ *   File Name: semaphore.c
+ *      Author: Zhao Yanbai
+ *              Sun Jun 22 13:57:18 2014
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+#include<semaphore.h>
+#include<irq.h>
+
+typedef struct semaphore_waiter
+{
+    list_head_t list;
+    task_union *task;
+    int up;
+} semaphore_waiter_t;
+
+#define SEMAPHORE_WAITER_INITIALIZER(name, task)    \
+{                                                   \
+    .list   = LIST_HEAD_INIT((name).list),          \
+    .task   = task,                                 \
+    .up     = 0                                     \
+}
+
+#define DECLARE_SEMAPHORE_WAITER(name, task)        \
+    semaphore_waiter_t name = SEMAPHORE_WAITER_INITIALIZER(name, task)
+
+
+void __down(semaphore_t *s)
+{
+    task_union *task = current;
+    DECLARE_SEMAPHORE_WAITER(waiter, task);
+    list_add_tail(&waiter.list, &s->wait_list);
+
+    while(true)
+    {
+        task->state = TASK_WAIT;
+
+        enable_irq();
+        schedule();
+        disable_irq();
+
+        if(waiter.up)
+            break;
+    }
+}
+
+void down(semaphore_t *s)
+{
+    unsigned long iflags;
+
+    irq_save(iflags);
+
+    if(likely(s->cnt>0))
+    {
+        s->cnt --;
+    }
+    else
+    {
+        __down(s);
+    }
+
+    irq_restore(iflags);
+}
+
+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;
+}
+
+
+void up(semaphore_t *s)
+{
+    unsigned long iflags;
+
+    irq_save(iflags);
+    if(likely(list_empty(&s->wait_list)))
+    {
+        s->cnt ++;
+    }
+    else
+    {
+        __up(s);
+    }
+
+    irq_restore(iflags);
+}
diff --git a/kernel/test_task.c b/kernel/test_task.c
deleted file mode 100644 (file)
index 1a15a16..0000000
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- *--------------------------------------------------------------------------
- *   File Name: test_task.c
- * 
- *      Author: Zhao Yanbai [zhaoyanbai@126.com]
- *              Tue Feb  2 20:18:05 2010
- * 
- * Description: none
- * 
- *--------------------------------------------------------------------------
- */
-
-
-#include <stdio.h>
-#include <sched.h>
-#include <assert.h>
-#include <system.h>
-task_union *    tTasks[NR_TASKS];
-
-#if 0
-void    SetuptTasks()
-{
-    int i;
-
-
-    for(i=0; i<NR_TASKS; i++)
-    {
-        tTasks[i] = NULL;
-    }
-
-    tTasks[0] = &RootTsk;
-    current    = tTasks[0];
-}
-#endif
-void    add_task(void *fun)
-{
-#if 0
-    assert(fun != NULL);
-    task_union *    tsk = NULL;
-    tsk = kmalloc_old(sizeof(Task));
-    if(tsk == NULL)
-        panic("shit happens");
-
-    printk("tsk:%08x\n", tsk);
-
-    tsk->pid    = get_next_pid();
-    tsk->ppid    = 0;
-    init_tsk_cr3(tsk);
-
-    pt_regs_t *    r;
-    r = &tsk->regs;
-    memset((void *)r, 0, sizeof(pt_regs_t));
-    r->ds = r->es = r->fs = r->gs = SELECTOR_USER_DS;
-    r->eip        = (unsigned long)fun;
-    r->cs        = SELECTOR_USER_CS;
-    r->eflags    = 0x282;
-    r->esp        = (unsigned long)tsk;
-    r->ss        = SELECTOR_USER_SS;
-
-    add_tsk2list(tsk);
-#endif
-}
-
-#if 0
-void    add_task(void *fun)
-{
-    assert(fun != NULL);
-    task_union *    tsk = NULL;
-    int i=0;
-    for(i=0; i<NR_TASKS; i++)
-    {
-        if(tTasks[i] == NULL)
-        {
-            tsk = kmalloc_old(sizeof(Task));
-            if(tsk == NULL)
-                panic("shit happens");
-            //tTasks[i] = tsk;
-            break;
-        }
-    }
-
-    if(i == NR_TASKS)
-        panic("tasks full");
-
-    pt_regs_t *    r;
-    r = &tsk->regs;//(pt_regs_t *)(TASK_SIZE + (unsigned long)tsk);
-    //printk("Add Tsk: tsk:%08x r:%08x ", tsk, r);
-    //r--;
-    //printk("r:%08x sizeof regs:%x ", r, sizeof(pt_regs_t));
-
-    memset((void *)r, 0, sizeof(pt_regs_t));
-    //printk("USER CS: %x\n", SELECTOR_USER_CS);
-    //printk("USER DS: %x\n", SELECTOR_USER_DS);
-    r->ds = r->es = r->fs = r->gs = SELECTOR_USER_DS;
-    r->eip        = (unsigned long)fun;
-    r->cs        = SELECTOR_USER_CS;
-    r->eflags    = 0x282;
-    r->esp        = (unsigned long)tsk;
-    r->ss        = SELECTOR_USER_SS;
-
-
-    tTasks[i] = tsk;
-}
-#endif
-
-void    delay(unsigned int d)
-{
-    unsigned int i;
-    int n = 10000;
-    for(i=0; i<d*n; i++)
-            ;
-}
-
-#if 0
-void    test_taskA()
-{
-    while(1)
-    {
-        printf("A");
-        delay(400);
-    }
-}
-
-void    test_taskB()
-{
-    while(1)
-    {
-        printf("B");
-        delay(500);
-    }
-}
-#endif
index 209631a51182a0aee1102169994d9bebbabb732b..6f785760f22c655ac4b68ab525efc2dd82320070 100644 (file)
  */
 #include <wait.h>
 
-void init_wait_queue(wait_queue_head_t * wqh)
+void init_wait_queue(wait_queue_head_t *wqh)
 {
-    INIT_LIST_HEAD(&wqh->wait);
+    INIT_LIST_HEAD(&wqh->task_list);
 }
 
-void add_wait_queue(wait_queue_head_t * wqh, wait_queue_t * wq)
+void add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
 {
-    list_add_tail(wq, &wqh->wait);
+    unsigned long iflags;
+    irq_save(iflags);
+    list_add_tail(&wq->task_list, &wqh->task_list);
+    irq_restore(iflags);
 }
 
-void del_wait_queue(wait_queue_head_t * wqh, wait_queue_t * old)
+void del_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
 {
-    //list_del_init();
+    unsigned long iflags;
+    irq_save(iflags);
+    list_del(&wq->task_list);
+    irq_restore(iflags);
 }
+
index 146cd0885a9cad30c669a198f85d27442ff15940..8c267665296afc7f2ca63e397e09d150b44585df 100644 (file)
@@ -82,7 +82,6 @@ void setup_kernel()
     
     void ide_init();
     ide_init();
-    ahci_init();
     printk("%s\n", version);