Embedded Xinu Kernel Programming

25
[email protected] BINA RAMAMURTHY Embedded Xinu Kernel Programming 5/18/2013 Amrita-UB-MSES-2013-11 1

description

Embedded Xinu Kernel Programming. [email protected] Bina Ramamurthy. 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 - PowerPoint PPT Presentation

Transcript of Embedded Xinu Kernel Programming

Page 1: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

1

[email protected] RAMAMURTHY

Embedded Xinu Kernel Programming

5/18/2013

Page 2: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

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=athlon64In general it defines various symbols;Various targets, dependencies, and commands

Target: dependenciescommand1command2…

Sometime commands are implied by the file extension of the dependencies

5/18/2013

Page 3: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

3

Include and lib directory

Include directoryAll 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 libraryLoader : usually written in assemble,

responsible for loading the OS. 5/18/2013

Page 4: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

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

5/18/2013

Page 5: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

5

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

5/18/2013

Page 6: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

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

5/18/2013

Page 7: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

7

resched(…)

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

5/18/2013

Page 8: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

8

yield(…)

int yield(void)A safe way of calling resched()First disables the interrupt request mask

and calls resched().Reference link

5/18/2013

Page 9: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

9

semaphore.h

Header file: sempaphore.hUsage:

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

5/18/2013

Page 10: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

10

sleep(…)

syscall sleep(uint ms)Put the calling thread to sleep for ms

milliseconds.Reference link

5/18/2013

Page 11: Embedded  Xinu  Kernel Programming

Resources & Critical Resources

5/18/2013Amrita-UB-MSES-2013-11

Shared resources: need mutual exclusionTasks cooperating to complete a jobTasks contending to access a resourceTasks synchronizing Critical resources and critical regionA important synchronization and mutual

exclusion primitive / resource is “semaphore”

Page 11

Page 12: Embedded  Xinu  Kernel Programming

Critical sections and Semaphores

5/18/2013Amrita-UB-MSES-2013-11

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

Page 12

Page 13: Embedded  Xinu  Kernel Programming

Semaphores

5/18/2013Amrita-UB-MSES-2013-11

See semaphore.h of xinu

Page 13

Page 14: Embedded  Xinu  Kernel Programming

Semaphore: wait()

5/18/2013Amrita-UB-MSES-2013-11

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; }

Page 14

Page 15: Embedded  Xinu  Kernel Programming

Semaphore: signal()

5/18/2013Amrita-UB-MSES-2013-11

• /*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;• }

Page 15

Page 16: Embedded  Xinu  Kernel Programming

Semaphore: usage

5/18/2013Amrita-UB-MSES-2013-11

• 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.

Page 16

Page 17: Embedded  Xinu  Kernel Programming

Problem 1

5/18/2013Amrita-UB-MSES-2013-11

#include <..>//declare semaphoresemaphore mutex1 = newsem(1);int counter = 0;//declare functions: proc1,proc1, proc3ready(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);

Page 17

Page 18: Embedded  Xinu  Kernel Programming

Problem 1: multi-tasks

5/18/2013Amrita-UB-MSES-2013-11

void proc1(){ while (1) { sleep (rand()%10); wait(mutex1); counter++; signal(mutex1);} } void proc2(){ while (1) { sleep (rand()%10); wait(mutex1); counter++; signal(mutex1);} } //similarly proc3

Page 18

Page 19: Embedded  Xinu  Kernel Programming

Problem 1

5/18/2013Amrita-UB-MSES-2013-11

Task 1 Task 2

Task 3

Counter1

Page 19

Page 20: Embedded  Xinu  Kernel Programming

Problem 2

5/18/2013Amrita-UB-MSES-2013-11

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);

Page 20

Page 21: Embedded  Xinu  Kernel Programming

Task flow

5/18/2013Amrita-UB-MSES-2013-11

void proc1()• { • while (1) {• sleep (rand()%10);• wait(synch31);• counter++;• signal(synch12);• } }

void proc2()• { • while (1) {• sleep (rand()%10);• wait(synch12);• counter++;• signal(synch23);• } }

void proc3() • { • while (1) {• sleep(rand()%10); • wait(synch23);• counter++;• signal(synch31); } }

Page 21

Page 22: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

22

Shell

5/18/2013

Shell provides the interface to the kernel from the nexos.cse.buffalo.edu “front-end” server

The wrt54gl are called “back-end” serversLook at the shell commands : shell.h, shell.cEach of the command is implemented in

xsh_name.cLets review some of them.

Page 23: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

23

TTY device

5/18/2013

The specification for tty is in include in tty.hThe function in tty.h are defined in the directory ttydevcall 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);

Page 24: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

24

UART

5/18/2013

This is an abstraction of the actual deviceuart.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);

Page 25: Embedded  Xinu  Kernel Programming

Amrita-UB-MSES-2013-11

25

Summary

5/18/2013

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.