Real-Time Operating Systems Laboratory Report

Salvatore Campione 145781 Vittorio Giovara 149374 Alberto Grand 149389 Academic Year 2008-2009

Contents
1 2 3 Introduction First Steps Using RTEMS 3.1 Operating System Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Sample Programs and Stack Overflows . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Custom Programs 4.1 Makefile and Defines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Manual Activation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3 Analysis of execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 3 5 5 5 7 7 7 8 12

4

A Test program – Readers and Writers

1

Chapter 1

Introduction
In this report we will present the workflow for configuring a simple concurrent program on an embedded system. The target architecture will be an ARM system, supported by the RTEMS operating systems; a cross toolchain will be used for preparing the code and simulate it. For test and simulation of the code we will use Skyeye program, while for compilation we will use a cross variant of the GNU Compiler Collection.

2

Chapter 2

First Steps
The first operation was to set up the correct environment for accessing the cross toolchain, installed in /opt/rtems-4.8; so we issued the following command: export PATH="/opt/rtems-4.8/bin:$PATH" After that we tried to compile with arm-rtems4.8-gcc a very simple C program that follows: #include <stdio.h> int main () { int a,b,c; a = 2; b = 3; c = a + b; return 0; } The code seemed to compile fine, but a warning was issued claiming that the START symbol was defaulted to 0x0008000: /opt/rtems-4.8/lib/gcc/arm-rtems4.8/4.2.2/../../../../arm-rtems4.8/bin/ld: warning: cannot find entry symbol _start; defaulting to 00008000 If we tried to ignore this warning and simulate it, the program would not execute. So having a look at the assembly code, obtained by means of arm-rtems4.8-objdump -d, we were hinted that the processor was starting to read the program from a location that contained only garbage code: Disassembly of section .text: 00008000 <rtems_provides_crt0>: 8000: e12fff1e bx lr 00008004 <malloc>: 3

8004: 8008: [...]

e3a00000 e12fff1e

mov bx

r0, #0 lr

; 0x0

In order to solve this problem we tried to force the START symbol to the location the processor was reading from first, and thus we modified the default linker configuration with this script: MEMORY { ram (W) : ORIGIN = 0x00000000, LENGTH = 0x00008000 } SECTIONS { . = 0x00000000; .text : { *(.text) } > ram _start = 0x00000000; } We generated the object files only with arm-rtems4.8-gcc -o file.o file.c and subsequently used arm-rtems4.8-ld ldscript.ld file.o, which read the configuration from the ldscript.ld file. Now the compilation didn’t issue any warning, so we addressed the Skyeye configuration. We initially set the configuration to the following: cpu: arm7tdmi mach: at91 mem_bank: map=M, type=RW, addr=0x00000000, size=0x00008000 We ran the compilation and tried to simulate, but Skyeye seemed stuck. Therefore, we debugged by means of arm-rtems4.8-gdb and inferred from the following message that the system was blocked on the first instruction: Single stepping until exit from function rtems_provides_crt0, which has no line number information. By inspecting the disassembled code again, we saw that garbage code was still found at the execution entry point. At this stage, we realised that the startup code, which should have been provided by RTEMS, was missing: without some code for allocating memory, stacks and I/O segments the application we developed would never have worked at all (on any system). This piece of code is either written by the developer or is provided by the operating systems: this is why we have to configure and use RTEMS as a base for developing our programs.

4

Chapter 3

Using RTEMS
3.1 Operating System Compilation

Once we understood that some kind of operating system was nedeed for applications to work properly, our next target was to successfully compile RTEMS for our architecture, using an appropriate toolchain. After setting up the environment and reading the provided documentation, we went to the RTEMS source directory and first tried a default configuration; however, the code stopped compiling quite soon (as expected!), so we ran the proposed configuration: export PREFIX=./rtems-test ./configure --target=arm-rtems4.8 --disable-posix --disable-networking --disable-cxx --enable-rtemsbsp=rtl22x --prefix=$PREFIX but we soon found out that the board name was wrongly spelled and that the prefix installation location required an absolute path. So we corrected the configuration string and experienced a successful compilation. export PREFIX=/opt/arm-rtems4.8 ./configure --target=arm-rtems4.8 --disable-posix --disable-networking --disable-cxx --enable-rtemsbsp=rtl22xx --prefix=$PREFIX make && make install We noticed that in other compilation tests it was impossible to compile on a file system that didn’t support symbolic links and permissions (like FAT); moving the source and build directory to a file system like ext3 or reiserfs solves this kind of problems.

3.2

Sample Programs and Stack Overflows

Having succeeded in compiling the operating system, we were then ready to test some of the sample programs with Skyeye. At first Skyeye refused to work, due to a bad configuration file; looking on the Skyeye website we found the correct configuration for our board, which is reported below: cpu: arm7tdmi mach: lpc2210 mem_bank: map=M, type=RW, addr=0x00000000, size=0x00004000 5

mem_bank: mem_bank: mem_bank: mem_bank:

map=M, map=M, map=I, map=I,

type=RW, type=RW, type=RW, type=RW,

addr=0x81000000, addr=0x40000000, addr=0xE0000000, addr=0xF0000000,

size=0x08000000 size=0x00400000 size=0xFFFFFFF size=0xFFFFFFF

Now we faced a very odd behavior: some of the test programs ran fine, or with minor warnings about I/O addressing, while many others printed random error strings or just closed with unexpected results. Fearing some misconfigurations, we tried starting over on our computers, recompiling also the crosstoolchain from scratch, but with no avail. So we were suggested that we should try debugging our application again with the cross GNU Debugger (arm-rtems-4.8-gdb) and Skyeye launched in debug mode (skyeye -d). After some tinkering we were finally able to understand the problem: a stack overflow. Since our board is very simple, the Memory Management Unit is disconnected and so no memory protection is used. We noticed that many programs, after the initialization of the stack, soon filled up the reserved stack size and started overwriting other memory locations. As a matter of fact we gathered this set of information by debugging: stack type abort mode interrupt mode fast interrupt mode supervisor mode start address 0x81010250 0x810102A0 0x810103A0 0x810103F0 end address 0x810102A0 0x810103A0 0x810103F0 0x810104F0 stack size 80 B 256 B 80 B 256 B

from which we deduced that in our configuration all programs are executed in supervisor mode, having access to all available system directives, and that of the 0x100 memory location of the supervisor stack, 0x64 entries are protected, leaving only 0x9C of available stack space. These values were due to the source files given for the first compilation. So in order to see if an actual stack overflow was generated, we set up a watchpoint in the debugger like the following: watch ( $sp < 0x810103F0 ) where sp is the stack pointer of the segment. After initialization and some iterations of the program, the watchpoint was activated and reported that an actual stack overflow was present; subsequently the program began its random behavior. So we thought that the size of the main stack was too small and tried increasing it by modifying the linkcmds file of our board, where all startup symbols are defined. Setting a stack size of 0xF00 bytes and recompiling from scratch provided us with a working toolchain capable of running all the sample programs available.

6

Chapter 4

Creating Custom Programs
4.1 Makefile and Defines

We tried the Readers and Writers paradigm as our test program: studying the main scheme presented during lessons we created our version of the problem using RTEMS directives. The Makefile we wrote was taken from one of the sample programs and modified accordingly; exporting the necessary environment variable export RTEMS_MAKEFILE_PATH=/opt/rtems-4.8/arm-rtems/rtl22xx/ allowed us to skip inserting manually the included libraries and to invoke directly the make command for quick compilation.

4.2

Manual Activation

After writing our program we compiled it with no errors or warnings. However, only the main thread (or “task”, in RTEMS nomenclature) was executed while all the other tasks did not start. We discovered that our programming procedure was not suited for embedded systems; as a matter of fact, we were used to take for granted very important components like the scheduler, the counters, even the clock, but in real time operating systems these units must be activated manually. In order to activate these features, some proper #define’s must be set up within the code, namely #define CONFIGURE APPLICATION NEEDS CLOCK DRIVER activates the clock; #define CONFIGURE APPLICATION NEEDS CONSOLE DRIVER activates the ticker; #define CONFIGURE RTEMS INIT TASKS TABLE initializes the task table; #define CONFIGURE MAXIMUM TASKS (2*(N WRITER + N READER)) sets up the number of concurrent thread. The minimum number of tasks is overestimated, since RTEMS creates further tasks to provide timeslicing and other features.

7

4.3

Analysis of execution

The next step was to simulate the code we wrote in order to verify the correct execution of the program. A first analysis with 3 Writers and 5 Readers, with a buffer of 50 slots, was executed. The following output was obtained in response: Your elf file is little endian. uart_mod:0, desc_in:, desc_out:, converter: start addr is set to 0x81000000 by exec file. CCCCCCCCCCERROR:io_write a non-exsiting addr:addr = fffff140, data = 810012d0 ERROR:io_write a non-exsiting addr:addr = fffff144, data = 810012d0 ERROR:io_write a non-exsiting addr:addr = fffff148, data = 810012d0 ERROR:io_write a non-exsiting addr:addr = fffff14c, data = 810012d0 ERROR:io_write a non-exsiting addr:addr = fffff150, data = 810012d0 ERROR:io_write a non-exsiting addr:addr = fffff154, data = 810012d0 ERROR:io_write a non-exsiting addr:addr = fffff158, data = 810012d0 ERROR:io_write a non-exsiting addr:addr = fffff15c, data = 810012d0 ERROR:io_write a non-exsiting addr:addr = fffff160, data = 810012d0 ERROR:io_write a non-exsiting addr:addr = fffff164, data = 810012d0 ERROR:io_write a non-exsiting addr:addr = fffff168, data = 810012d0 ERROR:io_write a non-exsiting addr:addr = fffff16c, data = 810012d0 ERROR:io_write a non-exsiting addr:addr = e000403c, data = 0 Writer 0 ha scritto: 0 Writer 0 ha scritto: 3 Writer 0 ha scritto: 6 Writer 0 ha scritto: 9 Writer 0 ha scritto: 12 Writer 0 ha scritto: 15 Writer 0 ha scritto: 18 Writer 0 ha scritto: 21 Writer 0 ha scritto: 24 Writer 0 ha scritto: 27 Writer 0 ha scritto: 30 Writer 0 ha scritto: 33 Writer 0 ha scritto: 36 Writer 0 ha scritto: 39 Writer 0 ha scritto: 42 Writer 0 ha scritto: 45 Writer 0 ha scritto: 48 Writer 0 ha scritto: 51 Writer 0 ha scritto: 54 Writer 0 ha scritto: 57 Writer 0 ha scritto: 60 Writer 0 ha scritto: 63 Writer 0 ha scritto: 66 Writer 0 ha scritto: 69 Writer 0 ha scritto: 72 Writer 0 ha scritto: 75 Writer 0 ha scritto: 78 Writer 0 ha scritto: 81 8

Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Writer Reader Reader Reader Reader Reader Reader Reader Reader Reader Reader Writer Writer Writer Reader Reader Reader Reader Reader Writer Writer Writer Reader Reader Reader Reader Reader Writer

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 0 1 0 4 3 2 0 1 2 1 2 3 4 0 0 1 2 1 0 4 3 2 0

ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha

scritto: 84 scritto: 87 scritto: 90 scritto: 93 scritto: 96 scritto: 99 scritto: 102 scritto: 105 scritto: 108 scritto: 111 scritto: 114 scritto: 117 scritto: 120 scritto: 123 scritto: 126 scritto: 129 scritto: 132 scritto: 135 scritto: 138 scritto: 141 scritto: 144 scritto: 147 letto: 0 letto: 3 letto: 6 letto: 9 letto: 12 letto: 15 letto: 18 letto: 21 letto: 24 letto: 27 scritto: 150 scritto: 1 scritto: 2 letto: 30 letto: 33 letto: 36 letto: 39 letto: 42 scritto: 153 scritto: 4 scritto: 5 letto: 45 letto: 48 letto: 51 letto: 54 letto: 57 scritto: 156

9

Writer Writer Reader Reader Reader Reader Reader Writer Writer Writer Reader Reader Reader Reader Reader Writer Writer Writer Reader Reader Reader Reader Reader Writer Writer Writer Reader Reader Reader Reader Reader Writer Writer Writer Reader Reader Reader Reader Reader Writer Writer Writer Reader Reader Reader Reader Reader

1 2 1 2 3 4 0 0 1 2 1 0 4 3 2 0 1 2 1 2 3 4 0 0 1 2 1 0 4 3 2 0 1 2 1 2 3 4 0 0 1 2 1 0 4 3 2

ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha ha

scritto: 7 scritto: 8 letto: 60 letto: 63 letto: 66 letto: 69 letto: 72 scritto: 159 scritto: 10 scritto: 11 letto: 75 letto: 78 letto: 81 letto: 84 letto: 87 scritto: 162 scritto: 13 scritto: 14 letto: 90 letto: 93 letto: 96 letto: 99 letto: 102 scritto: 165 scritto: 16 scritto: 17 letto: 105 letto: 108 letto: 111 letto: 114 letto: 117 scritto: 168 scritto: 19 scritto: 20 letto: 120 letto: 123 letto: 126 letto: 129 letto: 132 scritto: 171 scritto: 22 scritto: 23 letto: 135 letto: 138 letto: 141 letto: 144 letto: 147

As you can see looking at the report above, the timeslice needs some time to start; this might be due to the fact that each task has to execute its startup code, which can be somehow big, before starting the 10

job for which it was created. In fact, after some time, timeslicing starts working. Looking at the correctness of the execution, we can observe that the written and the read sequences in the buffer are {0 3 6 9 12 15 18 ...}, pointing out that the solution is correct; moreover, the flow of readers and writers is not constant (i.e. W0, W1, W2, R0, R1, etc.) but it depends on the timeslicing scheduler, which can easily be noticed looking at the report. Although our implementation of “readers and writers” appeared to work, a sequence of errors occurred at the beginning of the simulation with SkyEye. These errors pointed out that SkyEye was not able to write to some specific address locations, which corresponded to I/O addresses. By reading the provided documentation, we found out that only 16 vectored interrupts are initialized by RTEMS. However, the maximum number of interrupts (BSP MAX INT was set to 28 in the irq.h header file. We tried to change this value to 16 and recompile RTEMS. To our great satisfaction, the errors now disappeared: Your elf file is little endian. uart_mod:0, desc_in:, desc_out:, converter: start addr is set to 0x81000000 by exec file. CCCCCCCCCC Writer 0 ha scritto: 0 Writer 0 ha scritto: 3 Writer 0 ha scritto: 6 Writer 0 ha scritto: 9 Writer 0 ha scritto: 12 Writer 0 ha scritto: 15 Writer 0 ha scritto: 18 [...]

11

Appendix A

Test program – Readers and Writers
#include <bsp.h> #include <rtems.h> #include <stdlib.h> #include <stdio.h> #define MAXBUF 50 #define N_WRITER 10 #define N_READER 20 rtems_task reader(int); rtems_task writer(int); void enter_reader(void); void enter_writer(void); void exit_reader(void); void exit_writer(void); int count = 0; int rc = 0; rtems_id mutex_id /* protects rc */, db_id /* protects buffer */, ts_id /* avoids starvation */, empty_id /* allow writer */, full_id /* allow reader */; int buffer[MAXBUF]; int in_ptr = 0, out_ptr = 0; rtems_id readers_id[N_READER]; rtems_id writers_id[N_WRITER]; rtems_task Init(rtems_task_argument ignored) { int i, flag; rtems_time_of_day time; int code; time.year = 2008; time.month = 12; time.day = 16; time.hour = 9; time.minute = 0; time.second = 0;

12

time.ticks = 0; if (rtems_clock_set(&time) != RTEMS_SUCCESSFUL) { printf("Failure upon rtems_clock_set call\n"); exit(3); } if (rtems_semaphore_create(rtems_build_name(’m’, ’u’, ’t’, ’x’), 1, RTEMS_COUNTING_SEMAPHORE, 0, &mutex_id) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"mutex\" creation\n"); exit(1); } if (rtems_semaphore_create(rtems_build_name(’d’, ’b’, ’\0’, ’\0’), 1, RTEMS_COUNTING_SEMAPHORE, 0, &db_id) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"db\" creation\n"); exit(1); } if (rtems_semaphore_create(rtems_build_name(’t’, ’s’, ’\0’, ’\0’), 1, RTEMS_COUNTING_SEMAPHORE, 0, &ts_id) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"ts\" creation\n"); exit(1); } if (rtems_semaphore_create(rtems_build_name(’e’, ’m’, ’p’, ’\0’), MAXBUF, RTEMS_COUNTING_SEMAPHORE, 0, &empty_id) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"empty\" creation\n"); exit(1); } if (rtems_semaphore_create(rtems_build_name(’f’, ’u’, ’l’, ’\0’), 0, RTEMS_COUNTING_SEMAPHORE, 0, &full_id) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"full\" creation\n"); exit(1); } /* create reader processes */ for (i = 0; i < N_READER; i++) { if (rtems_task_create(rtems_build_name(’r’, ’e’, ’0’ + i, ’\0’), 1, RTEMS_MINIMUM_STACK_SIZE *2, RTEMS_DEFAULT_MODES | RTEMS_TIMESLICE, RTEMS_DEFAULT_ATTRIBUTES, &readers_id[i]) != RTEMS_SUCCESSFUL) { printf("Failure upon creation of reader %d\n", i); exit(2); } }

13

/* create writer processes */ for (i = 0; i < N_WRITER; i++) { if ((code = rtems_task_create(rtems_build_name(’w’, ’r’, ’0’ + i, ’\0’), RTEMS_MINIMUM_STACK_SIZE *2, RTEMS_DEFAULT_MODES | RTEMS_TIMESLICE, RTEMS_DEFAULT_ATTRIBUTES, &writers_id[i])) != RTEMS_SUCCESSFUL) { printf("Failure upon creation of writer %d, code = %d\n", i, code); exit(2); } } /* start processes */ flag = N_READER > N_WRITER; for (i = 0; i < (!flag ? N_READER : N_WRITER); i++) { if (rtems_task_start(readers_id[i], reader, i) != RTEMS_SUCCESSFUL) { printf("Failure upon start of reader %d\n", i); exit(2); } if (rtems_task_start(writers_id[i], writer, i) != RTEMS_SUCCESSFUL) { printf("Failure upon start of writer %d\n", i); exit(2); } }

for (i = (!flag ? N_READER : N_WRITER); i < (flag ? N_READER : N_WRITER); i++ { if (flag) { if (rtems_task_start(readers_id[i], reader, i) != RTEMS_SUCCESSFUL) { printf("Failure upon start of reader %d\n", i); exit(2); } } else { if (rtems_task_start(writers_id[i], writer, i) != RTEMS_SUCCESSFUL) { printf("Failure upon start of writer %d\n", i); exit(2); } } } rtems_task_delete( RTEMS_SELF ); }

14

rtems_task reader(int n) { int i; while (1) { enter_reader(); printf("Reader %d ha letto: %d\n", n, buffer[out_ptr]); out_ptr = (out_ptr + 1) % MAXBUF; usleep(200000); exit_reader(); } } rtems_task writer(int n) { int d = 0, i; while (1) { enter_writer(); buffer[in_ptr] = n+d; printf("Writer %d ha scritto: %d\n", n, buffer[in_ptr]); in_ptr = (in_ptr + 1) % MAXBUF; d+= N_WRITER; exit_writer(); } }

void enter_reader(void) { if (rtems_semaphore_obtain(full_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"full\" acquisition\n"); exit(1); } if (rtems_semaphore_obtain(ts_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"ts\" acquisition\n"); exit(1); } if (rtems_semaphore_release(ts_id) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"ts\" release\n"); exit(1);

15

} if (rtems_semaphore_obtain(mutex_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"mutex\" acquisition\n"); exit(1); } rc++; if (rc == 1) if (rtems_semaphore_obtain(db_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"db\" acquisition\n"); exit(1); } if (rtems_semaphore_release(mutex_id) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"mutex\" release\n"); exit(1); } } void exit_reader(void) { if (rtems_semaphore_obtain(mutex_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"mutex\" acquisition\n"); exit(1); } rc--; if (rc == 0) if (rtems_semaphore_release(db_id) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"db\" release\n"); exit(1); } if (rtems_semaphore_release(mutex_id) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"mutex\" release\n"); exit(1); } if (rtems_semaphore_release(empty_id) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"empty\" release\n"); exit(1); } }

16

void enter_writer(void) { if (rtems_semaphore_obtain(empty_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"empty\" acquisition\n"); exit(1); } if (rtems_semaphore_obtain(ts_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"ts\" acquisition\n"); exit(1); } if (rtems_semaphore_obtain(db_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"db\" acquisition\n"); exit(1); } } void exit_writer(void) { if (rtems_semaphore_release(full_id) != RTEMS_SUCCESSFUL) { printf("Failure upon semaphore \"full\" release\n"); exit(1); } if (rtems_semaphore_release(db_id) { printf("Failure upon semaphore exit(1); } if (rtems_semaphore_release(ts_id) { printf("Failure upon semaphore exit(1); } } != RTEMS_SUCCESSFUL) \"db\" release\n");

!= RTEMS_SUCCESSFUL) \"ts\" release\n");

/* configuration information */ #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER

17

#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER #define CONFIGURE_RTEMS_INIT_TASKS_TABLE #define CONFIGURE_MAXIMUM_TASKS (2*(N_WRITER + N_READER)) #define CONFIGURE_INIT #include <rtems/confdefs.h> /* end of file */

18

Sign up to vote on this title
UsefulNot useful