210 likes | 446 Views
DEV300: A Tiny CPU and OS in C#. Scott Hanselman Technology Evangelist/Architect Corillian Corporation scott@corillian.com. Tiny CPU and OS Design Goals. Creation of an Abstract Machine Simulate Running and Scheduling of Processes Provide Paging, Virtual Memory, Memory Protection
E N D
DEV300: A Tiny CPU and OS in C# Scott Hanselman Technology Evangelist/Architect Corillian Corporationscott@corillian.com
Tiny CPU and OSDesign Goals • Creation of an Abstract Machine • Simulate Running and Scheduling of Processes • Provide Paging, Virtual Memory, Memory Protection • Each Process scheduled independently • Inter-process Communication
Tiny CPU and OSDisclaimer • Goals • Meant to teach C# and exercise libraries • Meant to teach basic OS/CPU concepts • Meant to simulate OS behaviors • NOT GOALS • Not meant to get any work done • Not meant as a “perfect” simulation
CPU and OS • The CPU is a 32-bit machine • all addresses and registers are 32-bits • CPU • 10 registers 32-bits wide • IP – Instruction Pointer • SP – Stack Pointer • OS • 32 basic OpCodes • Each OpCode takes 1 clock cycle
Basic DesignObject mapping to actual OS/CPU pieces Program File on Disk 6 r4, $0 ;move 0 into r4 new Instruction(raw) Program Process in the OS InstructionCollection createProcess() Process Instruction Instruction Instruction Instruction
Operating System (at run-time) CPU Running Processes Collection Process Process Process Process Scheduler Loop Basic DesignObject mapping to actual OS/CPU pieces • Each Process scheduled independently • Poorly behaved processes are terminated • Memory is protected unless shared
CPU and OS • Executed like this from command-line: • C:> OS 2048 prog1.txt prog2.txt • Many Configuration options <configuration> <appSettings> <add key="PhysicalMemory" value="128" /> <add key="ProcessMemory" value="384" /> <add key="DumpPhysicalMemory" value="true" /> <add key="DumpInstruction" value="true" /> <add key="DumpRegisters" value="true" /> <add key="MemoryPageSize" value="16" /> <add key="StackSize" value="16" /> <add key="DataSize" value="16" /> </appSettings> </configuration>
Example Idle Loop 6 r4, $0 ;move 0 into r4 26 r4 ;lower priority TO 0 6 r1, $20 ;move 20 into r1 11 r1 ;print the number 20 6 r2, $-19 ;back up the ip 19 13 r2 ;loop (jump back 19) • Like a “Tiny Assembly Language” • Instructions are fetched from memory by the CPU and routed to the OS object • No compilation necessary – possible future enhancement?
Demo: Basic Programs • Manipulating Registers • Moving Memory, using the Stack • Small Functions • Inter-process Communication
Tiny OS Memory • “Physical Memory” just array of bytes • internal static byte[] physicalMemory; • Virtual Memory may be much larger! • But each Process has it’s own “view” • Virtual Memory hidden in code via [] Operator Overloading // Copy the code in one byte at a timeProcess p=new Process(++idPool, memSize);uint index = 0;foreach (byte b in processCode)memoryMgr[p.PCB.pid, index++] = b;
Tiny OS Memory Process Virtual Physical 654 456 0 123 0 543 001 1 255 255 1 2 310 635 456 2 255 3 456 Of course, the OS can address more memory than it has physically. So, you might have 256 bytes of memory, but can address 1024! 052 4 412 123 5 324 056 … 123 644
Tiny OS Memory Process Virtual Physical 654 456 0 123 0 543 001 1 255 255 1 2 310 635 456 2 255 3 456 052 4 412 All memory operations such as paging and swapping are hidden from the Process. 123 5 324 056 … 123 644
Tiny OS Memory with small physical memory and paging Process Virtual Physical 654 456 0 123 0 543 001 1 255 255 1 2 310 635 456 2 255 3 456 052 4 412 For the Process, everything stays the same, except now parts of virtual memory map to disk! <xml/> pages on disk 324 123 5 123 056 … 644
Major Elements (1 of 2) • Typed Collections • Generated by Tool • unsafe code • Like “inline C” • Delegates • Object-Oriented Function Pointers • Object Serialization • Memory Pages serialized to disk
Major Elements (2 of 2) • Custom Exceptions • Specific Derived Classes for each purpose • Regular Expressions • Simplifies Program Parsing • IComparable • Custom Sorting in Collections
Demo: Changing the OS • Changing Configuration Options • Modifying Virtual Memory Swapping! • Adjusting Memory Page Size • Analyzing Statistics and Profiling
SummaryTiny CPU and OS • Learn C# and .NET • Find a problem and solve it! • Make use of the .NET Framework Classes! • Imagine how you’d do it in other languages! • What .NET can do will surprise you!
Additional Resources • Get the Deep Technical Drill Down • DEV366: .NET Framework Under the Hood • Learn how to Architect an application • DEV310: Architecting Enterprise Applications with Visual Studio .NET • DEV358: Architecting N-Tier .NET Applications • DEV402: Design Choices for Implementing Distributed Applications in .NET • http://msdn.microsoft.com
© 2002 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.