*.BIN
*.map
*.diff
+*.swp
bin/hw
bin/sh
a.*
+++ /dev/null
-/*
- * ------------------------------------------------------------------------
- * 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()
-{
-
-
-
-}
#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;
printk(" ide status %02x pci status %02x\n", idest, pcist);
}
+
+
void ide_debug()
{
u32 device;
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);
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;
--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * 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))
+
*--------------------------------------------------------------------------
*/
-#ifndef _FS_H
+#ifndef _FS_H
#define _FS_H
#include <types.h>
#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)
{
return head->next == head;
}
+
*--------------------------------------------------------------------------
*/
-#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
--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * 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);
#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
};
{
unsigned long preempt_cnt;
-
unsigned long esp0; /* kernel stack */
/* for context switch */
list_head_t list;
- wait_queue_t wait;
-
pFile fps[NR_OPENS];
};
task_union *alloc_task_union();
-
static inline task_union *get_current()
{
task_union *tsk;
#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
#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);
+
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)
{
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;");
}
}
-task_union *get_unused_task_pcb()
-{
- unsigned int i;
- for(i=0; i<TASK_CNT; ++i)
- {
-
- }
-}
-
inline task_union *get_next_tsk()
{
return 0;
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;
}
--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * 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);
+}
+++ /dev/null
-/*
- *--------------------------------------------------------------------------
- * 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
*/
#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);
}
+
void ide_init();
ide_init();
- ahci_init();
printk("%s\n", version);