Anatomy of a Small Open-Source Kernel for DSPs
Like the kernel context data, you can find the context switch function in the file dsp_K.c, and it is called _DSP_K_SWITCH to avoid any potential confusion. It is first called from DSP_K_RUN to start multitasking and then evermore from DSP_K_TASK_SCHEDULE until your application ceases multitasking and returns via DSP_K_RUN to main. There are basically two routes through the kernel to reach DSP_K_TASK_SCHEDULE: either directly from a task function, such as DSP_K_TASK_PEND, or from the tick ISR. The context switch function behaves slightly differently depending upon which route is taken.
Wrapped up in C, the context switch function is mainly written in assembler (a summary in Listing 4 omits the assembler detail). It first performs some task lock checks and decides if a switch is actually necessary. If so, it gets a pointer to the current task context data structure described above and updates it from the RUN to the READY state (i.e., logically moves the task back onto the ready queue). It then sets about saving the current task context. Not shown in Listing 4, if the ISR route was followed, the code walks up the stack searching for the task frame before the interrupt was taken and flips the shadow registers used by the kernel in order to retrieve the task state.
With a little bit of juggling, saving the current running task context is fairly straightforward. Essentially, the general purpose registers are copied onto the current task context data structure followed by the general purpose index registers. Juggling is necessary to preserve registers until they are saved, to work with the shadow registers if the ISR route is followed and to collaborate with the compiler. (Depending upon the version of compiler you have, the executable code differs subtly, and you should make sure it behaves sensibly where clearly commented with VDSP_VER.) After saving the index registers, any of the optionally configured multiplier, modifier, base and length register values are copied onto the current task context stack, completing the first half of the context switch job.
The second half, recovering the new task context so that it is moved from READY to RUN and thus made current, is likewise fairly straightforward and works essentially in reverse of a context save.
Then in the third half, a C stack frame has to be set up. This is fiddly for two main reasons: first, the CPU registers have to be prepared in an orderly fashion, and second, the registers recovered in the second half must be preserved. (Again the details are omitted from Listing 4, but you can see them in the dsp_K.c file.) To prepare the CPU registers on a SHARC, the PC stack has to be emptied, the status and arithmetic status registers need to be set and the mode registers programmed in addition to setting up the PC, FP and SP registers. Finally, an ordinary return is executed to exit the context switcher and jump onto the new task frame.
Still with me? Well, we've just about covered the lowest sublayer functional services and can now move up a bit to the kernel personality services.
One facet of the process-oriented approach used in dsp_K is that tasks run forever (until explicitly halted by a call to DSP_K_TASK_EXIT or _exit), and the DSP_K_TASK_RESET function attends to this. A task stack is established in DSP_K_RUN so that, on reaching the end of its entry point code, your application tasks return to the DSP_K_TASK_RESET function. All this does is reset the task context to its initial conditions, and like the end of _DSP_K_SWITCH, it sets up a C stack frame and returns into the task entry point function.
Because tasks run forever, a mechanism for scheduling them needs to be provided. As you might have noticed when I introduced the BSP services above, the kernel services do not include a common scheduler. Rather, in the dsp_K model, task scheduling is carried out through programmer-provided functions attached to each task (refer to the t__DSP_K_TASK_DSCR.scheduler element in Listing 3). The dsp_K distribution includes classic round-robin and priority scheduler functions as generic scheduling methods, but you might want to write your own. The kernel uses the DSP_K_TASK_SCHEDULE function to call programmer-provided scheduler functions and then to check the sanity against preset limits so that the kernel won't crash before calling the context switcher.
With traditional UNIX or POSIX systems you can start tasks dynamically, so an unpredictable system lifetime exists for the kernel. By contrast, many embedded systems are designed for a specific purpose and to function statically. It is often well-stated that real-time schedulers should provide a hard real-time response to events (i.e., possess a low-interrupt latency). We could also consider that in order to build a predictable embedded system, deterministic scheduling is needed. The purpose of a scheduler function is to enable each task in your application to decide which one should be run next, and it is possible to provide a different scheduler function for each task within a dsp_K application. It is always possible for us to write poor applications that yield non-deterministic scheduling, but such design flaws are in our application code and not in the kernel.
Getting Started with DevOps - Including New Data on IT Performance from Puppet Labs 2015 State of DevOps Report
August 27, 2015
12:00 PM CDT
DevOps represents a profound change from the way most IT departments have traditionally worked: from siloed teams and high-anxiety releases to everyone collaborating on uneventful and more frequent releases of higher-quality code. It doesn't matter how large or small an organization is, or even whether it's historically slow moving or risk averse — there are ways to adopt DevOps sanely, and get measurable results in just weeks.
Free to Linux Journal readers.Register Now!
|Secure Server Deployments in Hostile Territory, Part II||Jul 29, 2015|
|Hacking a Safe with Bash||Jul 28, 2015|
|KDE Reveals Plasma Mobile||Jul 28, 2015|
|Huge Package Overhaul for Debian and Ubuntu||Jul 23, 2015|
|diff -u: What's New in Kernel Development||Jul 22, 2015|
|Shashlik - a Tasty New Android Simulator||Jul 21, 2015|
- Secure Server Deployments in Hostile Territory, Part II
- Hacking a Safe with Bash
- KDE Reveals Plasma Mobile
- Huge Package Overhaul for Debian and Ubuntu
- Home Automation with Raspberry Pi
- The Controversy Behind Canonical's Intellectual Property Policy
- Shashlik - a Tasty New Android Simulator
- Embed Linux in Monitoring and Control Systems
- diff -u: What's New in Kernel Development
- General Relativity in Python