CS452 - Real-Time Programming - Spring 2010

Public Interest Announcements

  1. Assignment 1 Deadline

Lecture 7 - Creating a Task

The Software Interrupt

What happens when the execution reaches a software interrupt instruction?

  1. The PSR is saved in spsr_svc.
  2. The address of the instruction immediately following swi is put in lr_svc
  3. In the PSR
  4. 0x08 is put in the pc.

This is done by the hardware; the rest is up to you.

In the calling code

   swi  n

In the kernel

The order matters

kernel entry:
  1. Change to system state
  2. Save the user state
  3. Return to supervisor mode
  4. Get the request
    ldr r3, [lr, #-4]
  5. Retrieve the kernel state, which does not include the scratch registers
  6. Put what you want in the task's TD
  7. Some where above you must have picked up the arguments
  8. Return from getNextRequest( active ) and get to work

After a while it's time to leave the kernel

  1. Schedule the next task to run
  2. Call GetNextRequest( active )
  3. Save kernel state on kernel stack
  4. From TD
  5. Set return value by overwriting r0 on user stack
  6. Switch to system mode
  7. Load registers from user stack
  8. Return to supervisor mode
  9. Let it go
    movs   pc, lr

Creating a Task

In creating a task you have to do two things

  1. Get and initialize resources needed by the task
  2. Make the task look as if it had just entered the kernel

Things you need to do

Get an unused TD and memory for its stack

Mostly filling in fields in the TD.

  1. task id
  2. stack pointer
  3. SPSR
  4. link register
  5. parent tid
  6. return value
  7. state
  8. install in the ready queues

Must also create the initial stack

The Create Function

You also need a int Create( int priority, void (*code) ( ) ) function to call from user tasks.

Although it's no more than a wrapper there are a few problems to solve.

  1. Passing arguments
  2. Jumping into the kernel
  3. Getting the return value from the kernel and returning it.

What follows just seems to say the same thing again. But we might as well leave it here because some student might find it useful.

How do we implement the API for user code?

  1. This requires a bit of assembly language.
  2. In assembly language all arguments & return values are words.
  3. What happens when Create is called?

What happens when Create returns?

When the caller is next activated,

user code has to get it and put it in the compiler's special place, r0 for gcc.

Return to: