1. The 38th
International convention MIPRO 2015
Computers in Technical Systems
Tiny Operating System Kernel for
Education Purposes
Samir Ribić, Adnan Salihbegović
Faculty of Electrical Engineering, Sarajevo
May 25-29, 2015 Opatija, Croatia
Computers in Education
2. • 3 basic styles of Operating systems courses
• User-oriented courses, inclining towards the practical
problems like installation, configuration and
administration of the operating systems (IT, IS).
• Theoretical analysis of algorithms that arise in the
implementation of operating systems is an approach
that is dominant in operating systems courses at the
universities oriented towards computer science (CS, SE).
• Explanation and source code development of an
operating system kernel offers better understanding of
OS fundamentals, but leaves less room for teaching
alternative approaches and examples (CS, CE).
Introduction
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 2
3. • Size matters! One megabyte=500 pages=full semester
• Code centric course or code as illustration?
• No full blown OS:
Linux 3.10 has 17 million lines of code,
Windows Research Kernel 83000 lines of code
Unix like kernels used in education:
Minix (16000 lines, complete), Xinu (8000, no file
system), Nachos (5000, OO virtual machine)
Old Unix versions
Linux 0.11 (14000), Qutenix (5500), XV6 (8300)
Not Unix like
GeekOS (3000, no FS), Black (CP/M like, 1200)
RELATED COURSES
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 3
4. • Standalone kernel capable to run under real hardware
(PC) and emulator
• 386-compatible PCs in 32 bit mode, with peripherals
that include an IDE drive, keyboard and text display.
• C language, when necessary assembly
• It was necessary to include in the kernel the range of
functionalities like: inputs/outputs, the organization of
memory, disk, file system and scheduling processes.
• Simplest algorithms in the realization of subsystems
• Layered architecture to follow the lessons: LEKOS
(Layered Educational Kernel of Operating System).
DESIGN GOALS
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 4
6. • Written in assembly language Max. size of 512 bytes
and finished with the signature. It consists of two main
parts, 16 bit and 32 bit.
• 16bit: executed at 000: 7c00 calling two times BIOS
interrupt 13h. The first call resets disk controller, the
second call transfers some sectors from the disk into the
RAM. With this transfer, the file with kernel is loaded
with into the memory. Segment GDTR register is loaded
to point to a table that simply maps the memory
segments for linear addressing.
• 32 bit: Program switches to protected mode, sets the
segment registers, stack pointer and jumps into the
kernel.
BOOT SECTOR
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 6
7. • The operating system is running on an empty machine.
Since the programmer has no standard C library at
disposal, several necessary functions for operation with
strings have to be provided. These functions can be
implemented purely in C language. The realized
functions are: strncmp, strncpy and itoa.
STRINGS
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 7
8. • The standard C language does not have built-in access
to I/O mapped peripherals. Therefore, it is necessary to
use assembler routines, but they are easily
encapsulated in the appropriate subroutines using inline
assembler built into the GCC.
unsigned char in(unsigned short _port){
unsigned char result;
__asm__("in %%dx,%%al": "=a" (result):"d" (_port));
return result;
}
I/O PORTS
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 8
9. • Textmode video buffer on PCs is located on the
absolute address 0xB8000. Access to the textmode
memory is in the form of a matrix with 80 columns, in
format of ASCII characters for every character on the
screen.
void Print(const char * msg) {
unsigned long i;
unsigned char *vmem;
vmem= (unsigned char *)0xB8000;
vmem += cursorpos<<1; i = 0;
while (msg[i] != 0) {
*vmem = msg[i++]; vmem += 2;
}
cursorpos += i;
hardwarecursor();
}
VIDEO
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 9
10. • The relatively easy part of this driver works directly
with scan codes. When the key is pressed (0x64h port
returns on zero bit the value of 1), the value of the
pressed key can be read from port 0x60h.
unsigned char GetScanCode(){
static char code;
if(in(0x64) & (0x01)) code = in(0x60);
else code = 0;
return code;
}
• Larger part of this driver is a subroutine that converts
the scanned code in ASCII, in cases with SHIFT, CTRL
combination and direct keypress.
KEYBOARD
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 10
11. • The next task in kernel development was to avoid busy
wait. For this purpose the interrupts are used. In
protected mode IDTR register points to the interrupt
vector table.
• The first of these is related to keyboard keypress
reading. Received scan code of the pressed key is placed
in a circular buffer.
• Second implemented interrupt is the timer interrupt. Its
task is simple, increases the counter by one every 10ms.
void Clock() {
__asm__ __volatile__("pushal"); elapsedtime++; out(0x20,0x20);
__asm__ __volatile__ (" popal ; leave ; iret");
}
INTERRUPTS
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 11
12. • IDE hard drive is used, because it is the simplest kind of
disk access at a low level. The ports 0x1f1 to 0x1f7 are
used to send LBA or CHS address of the first sector in a
group that user wants to transfer to RAM memory, with
the information how much sectors wants to transfer.
DISK DRIVER
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 12
13. • The largest subsystem, but still simple enough.
Therefore, the directory was implemented with only
one level (redy to be expanded), with a continuous
allocation of files on disk.
• Opening of the file copies directory element with
forwarded location number in the directory table, to the
free location in the table of open files
• Writing to a file: finding blocks on the disk, and then
the first of them is loaded into the buffer. Data is
transferred from the user buffer in the buffer disk block.
At the end of the buffer, the block is written to the disc.
Care that recorded blocks do not exceed the limit of file.
FILE SYSTEM
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 13
14. • int SaveDirEntry(unsigned int num)
• int OpenFileByNumber(int num)
• int OpenFile(char * name)
• int DeleteFile(char * name)
• int CreateFile(char * name, unsigned int maxsize)
• int CloseFile(int fd)
• int SeekFile(int fd, char * buffer,unsigned int position)
• int ReadFile(int fd, char * buffer,unsigned int size)
• int WriteFile(int fd, char * buffer,unsigned int size)
• int FormatPartition(int startsector, int maxfiles, int
partitionsize )
FILE SYSTEM
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 14
15. • In the operating system, memory between 0 and 640 KB
is reserved for kernel and its tables, while the custom
applications are allocated memory space above 1 MB.
• The memory management module is implemented with
two strategies, one for the physical memory and one for
virtual memory.
• The physical memory block is represented by
memTable element. First fit strategy
• Virtual memory uses 386 paging by mapping 4M logical
space to discontinual physical space
MEMORY MANAGER
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 15
16. • Cooperative multitasking
• setjmp macro makes context switch
• schedule() routine calls Round Robin
void schedule(void) {
unsigned prev;
static int cr0;
prev = current;
do {
current++; // round-robin switch to next task
if(current >= NUM_TASKS)
current = 0;
} while (g_tasks[current].state!=READY);
idlecpu=(prev==current==0);
if(setjmp(g_tasks[prev].regcontext) != 0)
return;
ActivatePaging ( g_tasks[current].pagetable);
longjmp(g_tasks[current].regcontext, 1);
}
PROCESS MANAGEMENT
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 16
Remember
Switch
17. • Since the scheduler is of the cooperative type, the
system calls are, beside the basic function, extended
with the call to scheduler, as in the following example:
int _OpenFile(char * name) {
int i=OpenFile(name);
schedule();
return i;
}
SYSTEM CALLS
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 17
18. • After several explained layers, main program started
with different test
TESTS
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 18
19. • 2 possible approaches:
•Change order of the course and introduce concepts
together with layers
•Classic order, and example in the last week
• Now used as example instead previously used oln linux
or Minix
• Much greater level of understanding
• 90 minutes enough to explain the whole source code
• Contribution to demistify Operating systems
CHANGES IN EDUCATION PROCESS
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 19
20. • Minimalist operating systems do not strive to be used in
real world applications, but can be of great importance
as an educational tool to display the principles of
operating systems structure and organization.
• Making use of the simplest algorithms, the developed
operating system kernel has fulfilled its objective and
purpose, to illustrate the practical side of the operating
systems role and inner working, while not spending too
much time on its explanation, at the expense of
theoretical considerations and approaches.
Conclusion
May 28, 2015 Ribić-Salihbegović/ETF Sarajevo 20