Testing the SAM R21 Xplained Pro

Posted by on 10:33 pm in SAM4S Xpro | 3 comments

These are a few tests I made with the SAM R21 Xplained Pro and an Xbee. The examples were generated from the Atmel Studio IEEE 802.15.4 MAC Wireless Wizard, with a few extra modifications (since they did not compile). The entire Atmel Studio projects used are available in the download area (tested with ASF v3.22.0). The tests include: sending, sending and receiving, reading LQI, and transmitting values read from the ADC.     Downloads  ...

read more

Buffer Overflow Exploit Demo (SAM4S and Xbee)

Posted by on 8:20 am in MiniOS, SAM4S Xpro, Xbee | 0 comments

I was thinking how to better illustrate (to students) the use of the stack for holding automatic variables at the machine level, and this came out. The class was using the SAM4S Xplained Pro board to learn ARM Thumb assembly, hence its use. The vulnerable application runs MiniOS.  

read more

Protected: Write yourself a Mini OS for the ARM Cortex-M4 (Scheduler)

Posted by on 1:24 pm in SAM4S Xpro | Enter your password to view comments.

    SCHEDULER   With the specific purpose of supporting OSes, the Cortex-M4 processor supplies three features. Namely,  supervisor calls (SVCs),  a SysTick timer,  and a pending supervisor call (PendSV). While SVCs  were used in the implementing of system calls, now we get to use the SysTick timer to assist us in theimplementation of an scheduler. PendSVs, on the contrary, are left out since I consider them not be necessary at all for our purposes.   SysTick Timer A SysTick Timer is a system timer intended to be used by operating systems as a system tick—meaning the tick signaling context switch in a multi-threaded environment. Unlike other non-system timers that may be added to the chip by microcontroller vendors, the SysTick can’t be modified by unprivleged code, and hence application code can’t mess with it. The SysTick is a 24-bit countdown timer. So basically we write a reload value between 0 and 2^24 in the system timer’s register holding the reload value. Then this value is decremented on every clock cycle (depending on which clock is configured as input), and when it reaches zero, a SysTick exception is generated. Then the count starts again from the reload value.  For more information see uC’s datasheet at pg. 209. From within the ASF we start the SysTick by calling Systick_Config(uint32_t ticks), defined in src/ASF/thirdparty/CMSIS/include/core_cm4.h. This is an example using a macro to set the interrupt to fire every 5ms:   #define MS_TO_TICKS(x)(sysclk_get_cpu_hz()/1000)*(x) SysTick_Config( MS_TO_TICKS(5) );   Note that since the processor clock is configured as input, we use sysclk_get_cpu_hz to get the clock frequency. To set the priority we may modify Systick_Config:   __STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks) { ... NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); ... }   The line  (1<<__NVIC_PRIO_BITS) – 1 evaluates to (1<<4)-1 = 16-1 = 15, which is the lowest possible interrupt priority (the higher the value, the lowest the priority). We’ll leave it as it is, because this is exactly what we want for the scheduler.   A single-threaded scheduler When dealing with not-so-easy problems, sometimes a good technique is to solve a simpler case. Assuming the simplification preserves the properties of the more complex problem, this normally should give us some insight into the problem and allows us to ignore certain details that would’ve obscure our reasoning otherwise. So we’ll start with the simplest scheduler that I was able to think of: an scheduler for one single thread. Among the set of things that we need to have in our scheduler, perhaps the most obvious is that of having a data structure for holding threads. To this end we define a new datatype MiniThread:   typedef struct { uint32_t id; //id char* name; //name of thread uint32_t* pStackFrame; //pointer to stack frame uint32_t* pStack; //pointer to top of the stack uint32_t* PC; //program counter }MiniThread;   MiniThread is a structure that holds all the information we need to manage threads. As far as out scheduler is concern, a thread is an instance of MiniThread. At any times we need to know the active thread, as well as have access to the list of all threads; even though in this case it’s just one. So we add the following static global variables:   static MiniThread threads[1]; static MiniThread* activeThread; //pointer to active thread   As you already know thread stacks must...

read more

An Array Monad in Haskell

Posted by on 10:49 am in Functional Programming | 0 comments

– This is an implementation of an array—using a size-balanced binary search tree under the hood—for a course in programming languages this summer. Some functions may look awkward as they are based on catamorphisms. Global definitions fromList, singleton, toList, set, get, size, contains, inverse, and trim   Test   As a functor   As a monad     Download   Download...

read more

Haskell State Monad Tutorial

Posted by on 9:44 pm in Functional Programming | 0 comments

Background The concept of state refers to a function/computation global or non-local state of memory at any given time. When the output of a function/computation depends solely on its inputs, we say it is stateless — an example of this is combinatory logic. Conversely, when the output depends not only on the received input but also on history of previous executions, we say it is stateful — this is the case in sequential logic. Notice variables local to some scope are not considered state as they die together with their scope and thus cannot keep history of previous executions. So we only consider global and non-local variables to be state. Imperative languages describe computations as series of modifications to state. Pure functional languages like Haskell, on the other hand, describe them as function evaluations and have no concept of state.  While being stateless comes with several advantages (I won’t talk about it), certain programs require to keep track of previous executions. The problem then becomes: how to implement state in a language that cannot, by definition, have state? We simulate it. From the point of view of the function caller — living in an outer scope than the function callee — functions being called can do only two things: take input, and return a value. Thereby the state has to come from input parameters, and can only survive a function return if returned by the callee once it has been altered. Then the caller is responsible for sequentially feeding the state to a function, collecting the new state from the callee upon evaluation, and feeding it to the next function in the execution chain. Let me illustrate it through an example.   Example Consider the following example, where we implement a tiny interpreter for a tiny language that supports only variable lookups and variable updates. To represent memory we use a list of pairs of strings, where the first element in the pair is the variable name, and the second one is the value for that variable name:   type Memory = [(String, String)]   We start by writing a function for looking up elements in memory — Given a variable name, and memory, we get its correspondent value (the second element in the pair whose first element matched the variable name).   varLookUpList :: String -> Memory -> Maybe String varLookUpList name [] = Nothing varLookUpList name ((n,v):xs) = if name == n then Just v else varLookUpList' name xs   Next we need to return the modified data structure to the caller so that the caller can pass it to the new callee, and thus preserve the modifications that have been made during the current call. Perhaps this is not the case when we are just “looking up” values, but in general it is. To do this we need an extra input to receive the state, and an extra output to give back the mutated state together with whatever output the function was returning anyway. In general we want to turn something like this (for functions that lookup values):   function :: a -> state -> b   or like this (for functions that alter a data structure):   function :: a -> state -> state   into something like this:   function :: a -> state ->...

read more