DEV300: A Tiny CPU and OS in C# Scott Hanselman Technology Evangelist/Architect Corillian...
-
Upload
johan-zachry -
Category
Documents
-
view
213 -
download
0
Transcript of DEV300: A Tiny CPU and OS in C# Scott Hanselman Technology Evangelist/Architect Corillian...
DEV300: DEV300: A Tiny CPU and OS in C#A Tiny CPU and OS in C#
Scott HanselmanScott HanselmanTechnology Evangelist/ArchitectTechnology Evangelist/ArchitectCorillian CorporationCorillian [email protected]@corillian.com
Tiny CPU and OSTiny CPU and OSDesign GoalsDesign Goals
Creation of an Abstract MachineCreation of an Abstract Machine Simulate Running and Scheduling of Simulate Running and Scheduling of
ProcessesProcesses Provide Paging, Virtual Memory, Provide Paging, Virtual Memory,
Memory ProtectionMemory Protection Each Process scheduled independentlyEach Process scheduled independently Inter-process CommunicationInter-process Communication
Tiny CPU and OSTiny CPU and OSDisclaimerDisclaimer
GoalsGoals Meant to teach C# and exercise librariesMeant to teach C# and exercise libraries Meant to teach basic OS/CPU conceptsMeant to teach basic OS/CPU concepts Meant to simulate OS behaviorsMeant to simulate OS behaviors
NOT GOALSNOT GOALS Not meant to get any work doneNot meant to get any work done Not meant as a “perfect” simulationNot meant as a “perfect” simulation
CPU and OSCPU and OS
The CPU is a 32-bit machine The CPU is a 32-bit machine all addresses and registers are 32-bits all addresses and registers are 32-bits
CPUCPU 10 registers 32-bits wide10 registers 32-bits wide IP – Instruction PointerIP – Instruction Pointer SP – Stack PointerSP – Stack Pointer
OSOS 32 basic OpCodes32 basic OpCodes Each OpCode takes 1 clock cycleEach OpCode takes 1 clock cycle
Basic DesignBasic DesignObject mapping to actual OS/CPU piecesObject mapping to actual OS/CPU pieces
ProgramProgram
InstructionInstructionCollectionCollection
InstructionInstruction
InstructionInstruction
InstructionInstruction
InstructionInstructionProcessProcesscreateProcess()createProcess()
6 r4, $0 ;move 0 into r46 r4, $0 ;move 0 into r4
new Instruction(raw)new Instruction(raw)
Program File on DiskProgram File on Disk
Process in the OSProcess in the OS
Basic DesignBasic DesignObject mapping to actual OS/CPU piecesObject mapping to actual OS/CPU pieces
CPUCPU
Operating System (at run-time)Operating System (at run-time)
Scheduler LoopScheduler Loop
Running Processes CollectionRunning Processes Collection
ProcessProcess ProcessProcess ProcessProcess ProcessProcess
Each Process scheduled independentlyEach Process scheduled independently Poorly behaved processes are terminatedPoorly behaved processes are terminated Memory is protected unless sharedMemory is protected unless shared
CPU and OSCPU and OS
Executed like this from command-line:Executed like this from command-line: C:> OS 2048 prog1.txt prog2.txtC:> OS 2048 prog1.txt prog2.txt
Many Configuration optionsMany Configuration options<configuration><configuration>
<appSettings><appSettings><add key="PhysicalMemory" <add key="PhysicalMemory"
value="128" />value="128" /><add key="ProcessMemory" value="384" /><add key="ProcessMemory" value="384" /><add key="DumpPhysicalMemory" <add key="DumpPhysicalMemory"
value="true" />value="true" /><add key="DumpInstruction" value="true" <add key="DumpInstruction" value="true"
/>/><add key="DumpRegisters" <add key="DumpRegisters"
value="true" />value="true" /><add key="MemoryPageSize" value="16" /><add key="MemoryPageSize" value="16" /><add key="StackSize" value="16" /><add key="StackSize" value="16" /><add key="DataSize" value="16" /><add key="DataSize" value="16" />
</appSettings></appSettings></configuration></configuration>
Instruction Set ExampleInstruction Set ExampleOpcodeOpcode ## FormatFormat
Sleep 25 Sleep r1; Sleep the # of clock cycles as indicated in r1. Another process or the idle process must be scheduled at this point. If the time to sleep is 0, the process sleeps infinitely.
SetPriority 26 SetPriority r1; Set the priority of the current process to the value in register r1; See priorities discussion in Operating system design
Exit 27 Exit. This opcode is executed by a process to exit and be unloaded. Another process or the idle process must now be scheduled.
FreeMemory 28 FreeMemory r1; Free the memory allocated whose address is in r1.
MapSharedMem
29 MapSharedMem r1, r2; Map the shared memory region identified by r1 and return the start address in r2.
SignalEvent 30 SignalEvent r1; Signal the event indicated by the value in register r1.
WaitEvent 31 WaitEvent r1; Wait for the event in register r1 to be triggered. This results in context-switches happening.
Input 32 Input r1; read the next 32-bit value into register r1.
MemoryClear 33 MemoryClear r1, r2; set the bytes starting at address r1 of length r2 bytes to zero.
Example Idle LoopExample Idle Loop
66 r4, $0r4, $0 ;move 0 into r4;move 0 into r4
2626 r4r4 ;lower priority TO 0;lower priority TO 0
66 r1, $20 r1, $20 ;move 20 into r1;move 20 into r1
1111 r1r1 ;print the number 20;print the number 20
66 r2, $-19r2, $-19 ;back up the ip 19;back up the ip 19
1313 r2r2 ;loop (jump back 19);loop (jump back 19)
Like a “Tiny Assembly Language”Like a “Tiny Assembly Language” Instructions are fetched from memory Instructions are fetched from memory
by the CPU and routed to the OS object by the CPU and routed to the OS object No compilation necessary – possible future enhancement?No compilation necessary – possible future enhancement?
Demo: Basic ProgramsDemo: Basic Programs
Manipulating RegistersManipulating Registers Moving Memory, using the StackMoving Memory, using the Stack Small FunctionsSmall Functions Inter-process CommunicationInter-process Communication
Tiny OS MemoryTiny OS Memory ““Physical Memory” just array of bytesPhysical Memory” just array of bytes
internal static byte[] physicalMemory;internal static byte[] physicalMemory;
Virtual Memory may be much larger!Virtual Memory may be much larger! But each Process has it’s own “view”But each Process has it’s own “view”
Virtual Memory hidden in code via Virtual Memory hidden in code via [] Operator Overloading[] Operator Overloading
// Copy the code in one byte at a time// Copy the code in one byte at a timeProcess pProcess p == new new Process(++idPool, memSize);Process(++idPool, memSize);uint uint index = 0;index = 0;foreach foreach ((byte byte bb in in processCodeprocessCode))
memoryMgr[p.PCB.pid, index++] = b;memoryMgr[p.PCB.pid, index++] = b;
Tiny OS MemoryTiny OS Memory
1231230
2551 255
4564562
45645600
00100111
31031022
PhysicalPhysicalVirtualVirtualProcessProcess
25525533
05205244
12312355
056056……
635
456
412
324
123
644
543
654
Of course, the OS Of course, the OS can can address address more more memory than it memory than it has physically. has physically. So, you might So, you might have 256 bytes of have 256 bytes of memory, but can memory, but can address 1024!address 1024!
All memory All memory operations such operations such as paging and as paging and swapping are swapping are hidden from the hidden from the Process.Process.
Tiny OS MemoryTiny OS Memory
1231230
2551 255
4564562
45645600
00100111
31031022
PhysicalPhysicalVirtualVirtualProcessProcess
25525533
05205244
12312355
056056……
635
456
412
324
123
644
543
654
<xml/><xml/>pages pages on diskon disk
Tiny OS Memory Tiny OS Memory with small physical memory and pagingwith small physical memory and paging
1231230
2551 255
4564562
45645600
00100111
31031022
PhysicalPhysicalVirtualVirtualProcessProcess
25525533
05205244
12312355
056056……
635
456
412
324
123
644
543
654
For the Process, For the Process, everything stays everything stays the same, except the same, except now parts of now parts of virtual memory virtual memory map to disk!map to disk!
Tiny OS Memory Tiny OS Memory memory becomes fragmentedmemory becomes fragmented
Major Elements (1 of 2)Major Elements (1 of 2) Typed CollectionsTyped Collections
Generated by ToolGenerated by Tool
unsafe unsafe codecode Like “inline C”Like “inline C”
Delegates Delegates Object-Oriented Function PointersObject-Oriented Function Pointers
Object SerializationObject Serialization Memory Pages serialized to diskMemory Pages serialized to disk
Major Elements (2 of 2)Major Elements (2 of 2) Custom ExceptionsCustom Exceptions
Specific Derived Classes for each Specific Derived Classes for each purposepurpose
Regular ExpressionsRegular Expressions Simplifies Program ParsingSimplifies Program Parsing
IComparableIComparable Custom Sorting in CollectionsCustom Sorting in Collections
Demo: Changing the OSDemo: Changing the OS
Changing Configuration OptionsChanging Configuration Options Modifying Virtual Memory Swapping!Modifying Virtual Memory Swapping! Adjusting Memory Page SizeAdjusting Memory Page Size Analyzing Statistics and ProfilingAnalyzing Statistics and Profiling
SummarySummaryTiny CPU and OSTiny CPU and OS
Learn C# and .NETLearn C# and .NET Find a problem and solve it!Find a problem and solve it! Make use of the .NET Framework Classes!Make use of the .NET Framework Classes! Imagine how you’d do it in other Imagine how you’d do it in other
languages!languages!
What .NET can do will surprise you!What .NET can do will surprise you!
Additional ResourcesAdditional Resources
Get the Deep Technical Drill Down Get the Deep Technical Drill Down DEV366: .NET Framework Under the HoodDEV366: .NET Framework Under the Hood
Learn how to Architect an applicationLearn how to Architect an application DEV310: Architecting Enterprise DEV310: Architecting Enterprise
Applications with Applications with Visual Studio .NETVisual Studio .NET
DEV358: Architecting N-Tier .NET DEV358: Architecting N-Tier .NET ApplicationsApplications
DEV402: Design Choices for Implementing DEV402: Design Choices for Implementing Distributed Applications in .NETDistributed Applications in .NET
http://msdn.microsoft.comhttp://msdn.microsoft.com
© 2002 Microsoft Corporation. All rights reserved.© 2002 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.