Embedded Xinu Kernel Programming 1 BINABUFFALO EDU BINA
Embedded Xinu Kernel Programming 1 BINA@BUFFALO. EDU BINA RAMAMURTHY Amrita-UB-MSES-2013 -11 5/18/2013
How to analyze the kernel code? 2 � 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=athlon 64 In general it defines various symbols; Various targets, dependencies, and commands Target: dependencies command 1 command 2 … Sometime commands are implied by the file extension of the dependencies Amrita-UB-MSES-2013 -11 5/18/2013
Include and lib directory 3 �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-2013 -11 5/18/2013
System Directory 4 � 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-2013 -11 5/18/2013
create(…) 5 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-2013 -11 5/18/2013
ready(…) 6 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-2013 -11 5/18/2013
resched(…) 7 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-2013 -11 5/18/2013
yield(…) 8 int yield(void) �A safe way of calling resched() �First disables the interrupt request mask and calls resched(). �Reference link Amrita-UB-MSES-2013 -11 5/18/2013
semaphore. h 9 �Header file: sempaphore. h �Usage: semaphore s 1; s 1 = semcreate(1); wait(s 1); signal(s 1); // sem_t s 1; // sem_init(&s 1, 0, 1); // sem_wait(&s 1); // sem_post(&s 1); �Check out test/test_semaphore. c Amrita-UB-MSES-2013 -11 5/18/2013
sleep(…) 10 syscall sleep(uint ms) �Put the calling thread to sleep for ms milliseconds. �Reference link Amrita-UB-MSES-2013 -11 5/18/2013
Resources & Critical Resources Page 11 �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-2013 -11 5/18/2013
Critical sections and Semaphores Page 12 �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-2013 -11 5/18/2013
Semaphores Page 13 See semaphore. h of xinu Amrita-UB-MSES-2013 -11 5/18/2013
Semaphore: wait() Page 14 � ppcb->sem = sem; /* record semaphore id in pcb */ � � � enqueue(currpid, psem->queue); resched(); /* place in wait queue and reschedule */ } restore(ps); return OK; /* restore interrupts */ � } Amrita-UB-MSES-2013 -11 5/18/2013
Semaphore: signal() Page 15 • • • • • /*signal - signal a semaphore, releasing one waiting process, and block * @param sem id of semaphore to signal * @return 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-2013 -11 5/18/2013
Semaphore: usage Page 16 • Problem 1: – – – 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-2013 -11 5/18/2013
Problem 1 Page 17 #include <. . > //declare semaphore mutex 1 = newsem(1); int counter = 0; //declare functions: proc 1, proc 3 ready(create((void *)proc 1, INITSTK, INITPRIO, “PROC 1", , 2, 0, NULL), RESCHED_NO); ready(create((void *)proc 2, INITSTK, INITPRIO, “PROC 2", , 2, 0, NULL), RESCHED_NO); ready(create((void *)proc 3, INITSTK, INITPRIO, “PROC 3", , 2, 0, NULL), RESCHED_NO); Amrita-UB-MSES-2013 -11 5/18/2013
Problem 1: multi-tasks Page 18 void proc 1() { while (1) { sleep (rand()%10); wait(mutex 1); counter++; signal(mutex 1); }} void proc 2() { while (1) { sleep (rand()%10); wait(mutex 1); counter++; signal(mutex 1); }} //similarly proc 3 Amrita-UB-MSES-2013 -11 5/18/2013
Problem 1 Page 19 Task 1 Counter 1 Task 2 Task 3 Amrita-UB-MSES-2013 -11 5/18/2013
Problem 2 Page 20 semaphore synch 12 = newsem(0); semaphore synch 23 = newsem(0); semaphore synch 31 = newsem(0); ready(create((void *)proc 1, INITSTK, INITPRIO, “PROC 1", , 2, 0, NULL), RESCHED_NO); ready(create((void *)proc 2, INITSTK, INITPRIO, “PROC 2", , 2, 0, NULL), RESCHED_NO); ready(create((void *)proc 3, INITSTK, INITPRIO, “PROC 3", , 2, 0, NULL), RESCHED_NO); signal(synch 31); Amrita-UB-MSES-2013 -11 5/18/2013
Task flow Page 21 void proc 1() void proc 2() void proc 3() • • • • • { while (1) { sleep (rand()%10); wait(synch 31); counter++; signal(synch 12); }} Amrita-UB-MSES-2013 -11 { while (1) { sleep (rand()%10); wait(synch 12); counter++; signal(synch 23); }} { while (1) { sleep(rand()%10); wait(synch 23); counter++; signal(synch 31); } } 5/18/2013
Shell 22 �Shell provides the interface to the kernel from the nexos. cse. buffalo. edu “front-end” server �The wrt 54 gl 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-2013 -11 5/18/2013
TTY device 23 �The specification for tty is in include in tty. h �The function in tty. h are defined in the directory tty �devcall tty. Init(device *); �devcall tty. Open(device *, va_list); �devcall tty. Close(device *); �devcall tty. Read(device *, char *, ushort); �devcall tty. Write(device *, uchar *, ushort); �devcall tty. Get. Char(device *); �devcall tty. Put. Char(device *, uchar); �devcall tty. Control(device *, uchar, uchar); Amrita-UB-MSES-2013 -11 5/18/2013
UART 24 �This is an abstraction of the actual device �uart. h is in include �devcall uart. Init(device *); �devcall uart. Read(device *, unsigned char *, int); �devcall uart. Write(device *, unsigned char *, int); �devcall uart. Get. Char(device *); �devcall uart. Put. Char(device *, unsigned char); �devcall uart. Control(device *, int, unsigned char); �interrupt uart. Intr(void); Amrita-UB-MSES-2013 -11 5/18/2013
Summary 25 �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-2013 -11 5/18/2013
- Slides: 25