CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2)...

40
Operating Systems In Depth VI–1 Copyright © 2020 Thomas W. Doeppner. All rights reserved. CS 167: I/O and Booting

Transcript of CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2)...

Page 1: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–1 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

CS 167: I/O and Booting

Page 2: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–2 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Simple I/O Architecture

Bus

ControllerController

Memory

Controller

DiskProcessor

Page 3: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–3 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

PIO Registers

Control registerGoR GoW IER IEW

Status registerRdyR RdyW

Read register

Write register

GoR Go read (start a read operation)

GoW Go write (start a write operation)

IER Enable read-completion interruptsIEW Enable write-completion interrupts

RdyR Ready to read

RdyW Ready to write

Legend:

Page 4: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–4 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Programmed I/O

• E.g.: Terminal controller• Procedure (write)

– write a byte into the write register– set the WGO bit in the control register– wait for WREADY bit (in status register) to be

set (if interrupts have been enabled, an interrupt occurs when this happens)

Page 5: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–5 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

DMA RegistersOp Code Control registerGo IE

Status registerRdy

Memory address register

Device address register

Go Start an operation

Op Code Operation code (identifies the operation)IE Enable interruptsRdy Controller is ready

Legend:

Page 6: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–6 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Direct Memory Access

• E.g.: Disk controller• Procedure

– set the disk address in the device address register (only relevant for a seek request)

– set the buffer address in the memory address register

– set the op code (SEEK, READ or WRITE), the GO bit and, if desired, the interrupt ENABLE bit in the control register

– wait for interrupt or for READY bit to be set

Page 7: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–7 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Device Drivers

Device Driver

CommonData

read

write

interrupt

Device

Page 8: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–8 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Page 9: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–9 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

PDP-8

Page 10: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–10 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

PDP-8 Boot Code

07756 6032 KCC07757 6031 KSF07760 5357 JMP .-107761 6036 KRB07762 7106 CLL RTL07763 7006 RTL07764 7510 SPA07765 5357 JMP 775707766 7006 RTL07767 6031 KSF07770 5367 JMP .-107771 6034 KRS07772 7420 SNL07773 3776 DCA I 777607774 3376 DCA 777607775 5356 JMP 775607776 0000 AND 007777 5301 JMP 7701

Page 11: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–11 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

VAX-11/780

Page 12: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–12 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

VAX-11/780 Boot

• Separate “console computer”– LSI-11– read boot code from floppy disk– load OS from root directory of first file system

on primary disk

Page 13: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–13 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Configuring the OS (1)

• Early Unix– OS statically linked to contain all needed

device drivers- all device-specific info included with

drivers– disk drivers contained partitioning description

Page 14: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–14 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Configuring the OS (2)

• Later Unix– OS statically linked to contain all needed

device drivers- at boot time, OS would probe to see which

devices were present and discover device-specific info

– partition table in first sector of each disk

Page 15: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–15 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

IBM PC

Page 16: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–16 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Issues

• Open architecture– large market for peripherals, most requiring

special drivers– how to access boot device?– how does OS get drivers for new devices?

Page 17: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–17 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

The Answer: BIOS

• Basic Input-Output System– code stored in non-volatile RAM

- CMOS, flash, whatever …– configuration data also in NV RAM

- including set of boot-device names– three primary functions

- power-on self test (POST)- load and transfer control to boot program- provide drivers for all devices

– main BIOS on motherboard– additional BIOSes on other boards

Page 18: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–18 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

POST

• On power-on, CPU executes BIOS code– located in last 64k of first megabyte of address

space– initializes hardware– counts memory locations

Page 19: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–19 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Getting the Boot Program

Boot Program

Partition Table

Magic Number

446 bytes

64 bytes

2 bytes

Partition 1Partition 2Partition 3Partition 4

Master Boot Record

Page 20: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–20 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Linux Booting (1)

• Two stages of booting provided by one of:– lilo (Linux Loader)

- uses sector numbers of kernel image– grub (Grand Unified Boot Manager)

- understands various file systems– both allow dual (or greater) booting

- select which system to boot from menu• perhaps choice of Linux or Windows

Page 21: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–21 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Linux Booting (2)

• Kernel image is compressed– step 1: set up stack, clear BSS,

uncompress kernel, then transfer control to it

• Process 0 is created– step 2: set up initial page tables,

turn on address translation• Do further initialization

– step 3: initialize rest of kernel, create init process (#1)

– invoke scheduler

assembler code (startup_32)

assembler code (different

startup_32)

C code (start_kernel)

Page 22: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–22 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Beyond BIOS

• BIOS– designed for 16-bit x86 of mid 1980s

• Open Firmware– designed by Sun– portable

- drivers, boot code in Forth• compiled into bytecode

– used on non-Intel systems• UEFI (Unified Extensible Firmware Interface)

– improved BIOS originally from Intel– also uses bytecode

Page 23: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–23 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

UNIX Structure

Page 24: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–24 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

The Unix Address Space

text

data

bssdynamic

stack

Page 25: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–25 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Creating a Process: Before

fork( )

parent process

Page 26: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–26 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Creating a Process: After

fork( )// returns p

parent process

fork( )// returns 0

child process (pid = p)

Page 27: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–27 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Fork and Wait

short pid;

if ((pid = fork()) == 0) {

/* some code is here for the child to execute */

exit(n);

} else {

int ReturnCode;

while(pid != wait(&ReturnCode))

;

/* the child has terminated with ReturnCode as its

return code */

}

Page 28: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–28 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Process Control BlocksPID

Terminated children

Link

Return code

Process Control Block

PID

Terminated children

Link

Return codePID

Terminated children

Link

Return codeOther Stuff

Other Stuff

Other Stuff

Page 29: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–29 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Exec

int pid;

if ((pid = fork()) == 0) {

/* we’ll soon discuss what might take place before execis called */

execl("/home/twd/bin/primes", "primes", "300", 0);

exit(1);

}

/* parent continues here */

while(pid != wait(0)) /* ignore the return code */

;

Page 30: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–30 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Loading a New Image

exec(prog, args)

Beforeprog’s textprog’s dataprog’s bss

args

After

Page 31: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–31 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Quiz 1int A=0, B=0, C=0, D=0;

A=1;

if (fork() > 0) {B=1;

A=111;

} else {

C=2;if (fork() > 0) {

D=222;

} else {D=A+B+C;

// what value is now// in D for this process?

}}

exit(0);

Answer:a) 0b) 3c) 113d) indeterminate

Page 32: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–32 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Representing the Address Space

• Important component of a process is its address space

– how is it represented?• Can page tables represent a process’s

address space?

Page 33: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–33 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Simple User Address Space

text

data

bss & dynamic

stack

Page 34: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–34 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Address-Space RepresentationSomewhat Simplified

mm_structtask_struct

vm_area_struct0–7fff

x, shared

vm_area_struct8000–1afffrw, private

vm_area_struct1b000–1bfffrw, private

vm_area_struct7fffd000–7fffffff

rw, private

struct file

Page 35: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–35 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Adding a Mapped File

text

data

bss & dynamic

stack

mapped file

Page 36: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–36 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Address-Space Representation:More Areas

mm_structtask_struct

vm_area_struct0–7fff

x, shared

vm_area_struct8000–1afffrw, private

vm_area_struct1b000–1bfffrw, private

vm_area_struct200000–201fff

rw, shared

vm_area_struct7fffd000–7fffffff

rw, private

struct file struct file

Page 37: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–37 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Adding More Stuff

text

data

stack 2

bss & dynamic

stack 1

stack 3

mapped file 3

mapped file 117

...

mapped file 2mapped file 1

Page 38: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–38 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Address-Space Representation:Reality

mm_structtask_struct

200000–201fff

1b000–1bfff 202000–203fff

0–7fff 8000–1afff 7fffd000–7fffffff204000–204fff

208000–210fff

Page 39: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–39 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Layering

User Applications

System CallsUserKernel

Virtual File System (VFS)

disk file systems (e.g. S5FS)

terminals networkingother things (e.g. /proc)

Page 40: CS 167: I/O and Bootingcs.brown.edu/courses/cs167/lectures/07BootX.pdf · Linux Booting (2) •Kernel image is compressed –step 1: set up stack, clear BSS, uncompress kernel, then

Operating Systems In Depth VI–40 Copyright © 2020 Thomas W. Doeppner. All rights reserved.

Quiz 2int fd1 = open("file", O_CREAT|O_RDWR, 0666);

unlink("file");

write(fd1, "123", 3);int fd2 = open("file", O_CREAT|O_RDWR, 0666);

write(fd2, "4", 1);

if (fork() == 0) {

write(fd1, "5", 1);}

exit(0);

The final contents of file are:a) 12345b) 453c) 45d) 4