Presentation is loading. Please wait.

Presentation is loading. Please wait.

Embedded Xinu Kernel Programming

Similar presentations


Presentation on theme: "Embedded Xinu Kernel Programming"— Presentation transcript:

1 Embedded Xinu Kernel Programming
Bina Ramamurthy Amrita-UB-MSES 5/18/2013

2 How to analyze the kernel code?
Review the Makefile to understand the various modules involved Cross compiling done by specifying appropriate compile option. In this case –march=mips Another example : -march=athlon64 In general it defines various symbols; Various targets, dependencies, and commands Target: dependencies command1 command2 Sometime commands are implied by the file extension of the dependencies Amrita-UB-MSES 5/18/2013

3 Include and lib directory
Include directory All the header file… lets go through some of them clock.h gpio.h memory.h proc.h shell.h string.h vararg.h ctype.h interrupt.h mips.h queue.h stdio.h tty.h xc.h device.h kernel.h platform.h semaphore.h stdlib.h uart.h Load library: primarily c library Loader : usually written in assemble, responsible for loading the OS. Amrita-UB-MSES 5/18/2013

4 System Directory clockinit.c freemem.c initialize.c kprintf.c read.c send.c write.c clockintr.c freesem.c insert.c main.c ready.c signal.c xdone.c close.c getc.c insertd.c newsem.c receive.c signaln.c xtrap.c control.c getmem.c ioerr.c open.c resched.c sleep.c create.c getpid.c ionull.c putc.c scount.c wait.c devtable.c getstk.c kill.c queue.c seek.c wakeup.c Amrita-UB-MSES 5/18/2013

5 create(…) Creates a thread, similar to pthread_create.
tid_typ create(void *procaddr, uint ssize, int priority, char *name, int nargs, ...) Creates a thread, similar to pthread_create. Returns the thread ID. Takes in the function to be executed, stack size, priority, name of the thread, number of arguments for the function, argument 1, argument 2, … Reference link Amrita-UB-MSES 5/18/2013

6 ready(…) int ready(tid_typ tid, bool resch) Makes a thread (with thread ID == tid) eligible for CPU service. Takes in the thread ID, and resch = {RESCHED_YES, RESCHED_NO} Inserts the thread ID into the readylist, which is a FIFO queue. Calls resched() if resch == RESCHED_YES Reference link Amrita-UB-MSES 5/18/2013

7 resched(…) Reschedules processor to the highest priority-ready thread.
int resched(void) Reschedules processor to the highest priority-ready thread. Dequeues the thread with the highest priority from the readylist and changes its state to THRCURR. This thread will run next. Reference link Amrita-UB-MSES 5/18/2013

8 yield(…) A safe way of calling resched()
int yield(void) A safe way of calling resched() First disables the interrupt request mask and calls resched(). Reference link Amrita-UB-MSES 5/18/2013

9 semaphore.h Header file: sempaphore.h Usage:
semaphore s1; // sem_t s1; s1 = semcreate(1); // sem_init(&s1, 0, 1); wait(s1); // sem_wait(&s1); signal(s1); // sem_post(&s1); Check out test/test_semaphore.c Amrita-UB-MSES 5/18/2013

10 sleep(…) Put the calling thread to sleep for ms milliseconds.
syscall sleep(uint ms) Put the calling thread to sleep for ms milliseconds. Reference link Amrita-UB-MSES 5/18/2013

11 Resources & Critical Resources
Shared resources: need mutual exclusion Tasks cooperating to complete a job Tasks contending to access a resource Tasks synchronizing Critical resources and critical region A important synchronization and mutual exclusion primitive / resource is “semaphore” Amrita-UB-MSES 5/18/2013

12 Critical sections and Semaphores
When multiples tasks are executing there may be sections where only one task could execute at a given time: critical region or critical section There may be resources which can be accessed only be one of the processes: critical resource Semaphores can be used to ensure mutual exclusion to critical sections and critical resources Amrita-UB-MSES 5/18/2013

13 Semaphores See semaphore.h of xinu Amrita-UB-MSES 5/18/2013

14 Semaphore: wait() ppcb->sem = sem; /* record semaphore id in pcb */
enqueue(currpid, psem->queue); resched(); /* place in wait queue and reschedule */ } restore(ps); /* restore interrupts */ return OK; Amrita-UB-MSES 5/18/2013

15 Semaphore: signal() /*signal - signal a semaphore, releasing one waiting process, and block sem id of semaphore to signal OK on success, SYSERR on failure */ syscall signal(semaphore sem) { irqmask ps; register struct sentry *psem; ps = disable(); /* disable interrupts */ if ( isbadsem(sem) ) /* safety check */ restore(ps); return SYSERR; } psem = &semtab[sem]; /* retrieve semaphore entry */ if ( (psem->count++) < 0 ) /* release one process from wait queue */ { ready(dequeue(psem->queue), RESCHED_YES); } restore(ps); /* restore interrupts */ return OK; Amrita-UB-MSES 5/18/2013

16 Semaphore: usage Problem 1: Problem 2:
Create 3 tasks that each sleep for a random time and update a counter. Counter is the critical resources shared among the processes. Only one task can update the counter at a time so that counter value is correct. Problem 2: Create 3 tasks; task 1 updates the counter by 1 and then signal task 2 that updates the counter by 2 and then signals task 3 to update the counter by 3. Amrita-UB-MSES 5/18/2013

17 Problem 1 #include <..> //declare semaphore semaphore mutex1 = newsem(1); int counter = 0; //declare functions: proc1,proc1, proc3 ready(create((void *)proc1, INITSTK, INITPRIO, “PROC1",, 2, 0, NULL), RESCHED_NO); ready(create((void *)proc2, INITSTK, INITPRIO, “PROC2",, 2, 0, NULL), RESCHED_NO); ready(create((void *)proc3, INITSTK, INITPRIO, “PROC3",, 2, 0, NULL), RESCHED_NO); Amrita-UB-MSES 5/18/2013

18 Problem 1: multi-tasks void proc1() { while (1) { sleep (rand()%10); wait(mutex1); counter++; signal(mutex1); } } void proc2() //similarly proc3 Amrita-UB-MSES 5/18/2013

19 Problem 1 Task 1 Task 2 Counter1 Task 3 5/18/2013
Amrita-UB-MSES 5/18/2013

20 Problem 2 semaphore synch12 = newsem(0); semaphore synch23 = newsem(0); semaphore synch31 = newsem(0); ready(create((void *)proc1, INITSTK, INITPRIO, “PROC1",, 2, 0, NULL), RESCHED_NO); ready(create((void *)proc2, INITSTK, INITPRIO, “PROC2",, 2, 0, NULL), RESCHED_NO); ready(create((void *)proc3, INITSTK, INITPRIO, “PROC3",, 2, 0, NULL), RESCHED_NO); signal(synch31); Amrita-UB-MSES 5/18/2013

21 Task flow void proc1() void proc2() void proc3() 5/18/2013 {
while (1) { sleep (rand()%10); wait(synch31); counter++; signal(synch12); } } void proc2() wait(synch12); signal(synch23); void proc3() sleep(rand()%10); wait(synch23); signal(synch31); } } Amrita-UB-MSES 5/18/2013

22 Shell Shell provides the interface to the kernel from the nexos.cse.buffalo.edu “front-end” server The wrt54gl are called “back-end” servers Look at the shell commands : shell.h, shell.c Each of the command is implemented in xsh_name.c Lets review some of them. Amrita-UB-MSES 5/18/2013

23 TTY device The specification for tty is in include in tty.h
The function in tty.h are defined in the directory tty devcall ttyInit(device *); devcall ttyOpen(device *, va_list); devcall ttyClose(device *); devcall ttyRead(device *, char *, ushort); devcall ttyWrite(device *, uchar *, ushort); devcall ttyGetChar(device *); devcall ttyPutChar(device *, uchar); devcall ttyControl(device *, uchar, uchar, uchar); Amrita-UB-MSES 5/18/2013

24 UART This is an abstraction of the actual device uart.h is in include
devcall uartInit(device *); devcall uartRead(device *, unsigned char *, int); devcall uartWrite(device *, unsigned char *, int); devcall uartGetChar(device *); devcall uartPutChar(device *, unsigned char); devcall uartControl(device *, int, unsigned char, unsigned char); interrupt uartIntr(void); Amrita-UB-MSES 5/18/2013

25 Summary We looked the embedded xinu kernel.
Read it again to get a better in-depth understanding of the kernel. Now we will capture the whole picture of all the hardware and software combined in a class diagram. Amrita-UB-MSES 5/18/2013


Download ppt "Embedded Xinu Kernel Programming"

Similar presentations


Ads by Google