Vous êtes sur la page 1sur 130

BUENOS

is a University Educational Nutshell Operating System

BUENOS is a University Educational Nutshell Operating System Roadmap to the BUENOS system Version 1.1.1 October

Roadmap to the BUENOS system Version 1.1.1 October 5, 2007

Juha Aatrokoski, Timo Lilja, Leena Salmela, Teemu J. Takanen and Aleksi Virtanen

BUENOS is licenced under the following ”modified BSD license” (i.e., the BSD license without the advertising clause).

Copyright © 2003–2007 Juha Aatrokoski, Timo Lilja, Leena Salmela, Teemu J. Takanen and Aleksi Virtanen

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR “AS IS” AND ANY EX- PRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SER- VICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFT- WARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Contents

1 Introduction

 

1

1.1 Expected Background Knowledge

 

1

1.2 How to Use This Document

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2

1.3 BUENOS for teachers .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2

 

1.3.1

Preparing for BUENOS Course

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2

1.4 Exercises

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

3

1.5 Contact Information

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

3

2 Using Buenos

 

4

2.1 Installation and Requirements

 

.

.

.

.

.

.

.

.

.

4

2.2 Compilation .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

4

2.3 Booting the System

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

4

2.4 Compiling Userland Programs

 

.

.

.

.

.

.

.

.

.

5

2.5 Using the Makefiles .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

5

 

2.5.1 System Makefile

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

5

2.5.2 Userland Makefile

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

6

2.6 Using Trivial Filesystem

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

6

2.7 Starting Processes

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

7

3 Kernel Overview

 

8

3.1 Directory Structure .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

8

3.2 Kernel Architecture

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

9

 

3.2.1 Threading .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

10

3.2.2 Virtual Memory

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

11

3.2.3 Support for Multiple Processors

 

11

3.3

Kernel Programming .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

13

3.3.1

Memory Usage

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

13

3.3.2

Stacks and Contexts

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

13

3.3.3

Library Functions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

14

3.3.4

Using a Console

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

14

3.3.5

Busy Waiting

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

14

3.3.6

Floating Point Numbers

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

14

3.3.7

3.3.8

Naming Conventions

.

Debug Printing .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

14

14

3.3.9

C Calling Conventions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

15

3.3.10

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

15

Exercises

.

Kernel Boot Arguments .

.

.

.

.

.

.

.

.

.

.

.

.

4 Threading and Scheduling

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

15

16

4.1

Threads

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

16

4.1.1 Thread Table

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

17

4.1.2 Thread Library .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

17

4.2 Scheduler

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

20

4.2.1

Idle thread

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

21

4.3 Context Switch

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

21

4.3.1 Interrupt Vectors

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

22

4.3.2 Context Switching Code .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

23

4.3.3 Thread Contexts

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

24

4.4 Exception Processing in Kernel Mode

25

Exercises

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

25

5 Synchronization Mechanisms

27

5.1 Spinlocks

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

27

5.1.1 LL and SC Instructions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

27

5.1.2 Spinlock Implementation

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

28

5.2 Sleep Queue

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

28

5.2.1 Using the Sleep Queue .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

28

5.2.2 How the Sleep Queue is Implemented

.

5.3 Semaphores

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

30

31

5.3.1

Exercises

. Semaphore Implementation

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

6 Userland Processes

.

.

32

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

33

37

6.1 Process Startup .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

37

6.2 Userland Binary Format

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

38

6.3 Exception Handling

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

41

6.4 System Calls

. 6.4.1 How System Calls Work

.

.

.

.

.

.

.

.

.

Exercises

.

.

.

.

.

.

.

.

.

.

.

.

.

.

7 Virtual Memory

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

41

42

6.4.2 System Calls in BUENOS

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

42

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

45

48

7.1 Hardware Support for Virtual Memory

48

7.2 Virtual memory initialization

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

49

7.3 Page Pool

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

49

7.4 Pagetables and Memory Mapping

50

7.5

TLB

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

52

7.5.1

TLB dual entries and ASID in MIPS32 architectures

.

.

.

.

.

53

7.5.2

TLB miss exception, Load reference

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

53

7.5.3

TLB miss exception, Store reference

.

.

.

.

.

.

.

.

.

.

.

.

.

.

53

7.5.4

7.5.5

Exercises

.

8 Filesystem

. TLB wrapper functions in BUENOS

TLB modified exception

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

53

53

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

57

59

8.1 Filesystem Conventions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

59

8.2 Filesystem Layers .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

59

8.3 Virtual Filesystem

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

60

8.3.1 Return Values

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

60

8.3.2 Limits

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

61

8.3.3 Internal Data Structures .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

62

8.3.4 VFS Operations

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

62