Hacking GeekOS
David H. Hovemeyer, Jeffrey K. Hollingsworth, and Iulian Neamtiu
April 12, 2005
Copyright
c
2003, 2004, 2005 David H. Hovemeyer, Jeffrey K. Hollingsworth, and Iulian Neamtiu
Legal Notice
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike License. To
view a copy of this license, visit <http://creativecommons.org/licenses/by-nc-sa/1.0/> or
send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
Chapter 1
Introduction
GeekOS is an educational operating system kernel. GeekOS tries to combine realism and simplicity. It is a
realistic system because it targets a real hardware platform - the x86 PC. It strives for simplicitly in that it
contains the bare minimum functionality necessary to provide the services of a modern operating system,
such as virtual memory, a filesystem, and interprocess communication.
IMPORTANT
This document and the GeekOS distribution are works in progress. At the time of
writing (March 3, 2004) this document is about 80% complete, and the GeekOS
code is about 95% complete. We will be filling in the remaining text and code in the
near future. In the meantime, if you have any questions about this manual or about
GeekOS itself, please send email to daveho@cs.umd.edu <mailto:daveho@cs.
umd.edu>.
This document has two purposes. The first purpose is to give an overview of the GeekOS kernel, and to
cover the topics needed to read, understand, and modify the kernel source code. The second purpose is to
present a series of projects in which you can build important new functionality on top of the GeekOS kernel.
These projects are suitable for use in a senior level undergraduate course, or for self-study.
1.1 Intended Audience
This document is for anyone interested in gaining hands-on experience in operating system kernel program-
ming. Most operating system textbooks focus on high level theory and concepts. This document is intended
to bridge the gap between those concepts and actual, working kernel code. We will try to give you all of
the information and background you need to start hacking. In this way, this document complements your
operating system textbook.
1.2 Required background
Before you start hacking on GeekOS, we assume that you have the following skills and knowledge:
• Basic understanding of what an operating system kernel does
• A strong understanding of the C programming language
• Experience programming at the system call level in an operating system such as Linux or Windows
• Experience programming using threads, such as pthreads or Java threads
3
CHAPTER 1. INTRODUCTION 1.2. REQUIRED BACKGROUND
• Some knowledge of computer architecture and organization
• Familiarity with assembly language for some CPU architecture, and a willingness to learn x86 (a.k.a.
Intel IA32) assembly language
4
Chapter 2
Kernel Hacking 101
GeekOS is an operating system kernel. In many respects, it is simply a C program. It has functions, threads,
a memory allocator, and so forth. However, unlike a C program executing as a user mode process of a host
operating system such as Linux or Windows, a kernel operates in kernel mode. A program executing in kernel
mode has total control over the computer’s CPU, memory, and hardware devices.
Writing code to execute in kernel mode presents a few challenges that you will need to be aware of. This
chapter presents some important tips and techniques that you will need to use as you modify the GeekOS
kernel.
2.1 Kernel Mode Restrictions
The runtime environment of the GeekOS kernel has several important restrictions you will need to be aware
of.
2.1.1 Limited Set of Library Functions
Because the operating system is that lowest level of software in the computer, all functionality used by the
kernel must be implemented within the kernel. This is different than for user programs, which are generally
linked against a set of standard libraries containing often-used functions.
The only standard C library functions available in GeekOS are a subset of the string functions (strcpy(
), memcpy(), etc.) and the snprintf() function. Prototypes for these functions are defined in the header
<geekos/string.h>.
In addition to the standard C functions, the GeekOS kernel also contains functions which are similar to
C library functions. The Print() function (prototype in <geekos/screen.h>) is a subset of the standard
C printf() function. The Malloc() and Free() functions (prototypes in <geekos/malloc.h>) are
equivalent to the malloc() and free() functions.
2.1.2 Limited Stack
Each thread in the GeekOS kernel has a 4K stack. If a thread overflows its stack, a kernel crash will generally
be the result. Therefore, you should be very careful to conserve stack space:
• Do not allocate large data structures on the stack. Use the kernel heap allocator (Malloc() and Free(
)) instead.
• Do not use recursion. Avoid deep call stacks.
5
评论0
最新资源