Tiny CPU and OS Design Goals Creation of an Abstract Machine Creation of an Abstract Machine Simulate Running and Scheduling of Processes Simulate Running and Scheduling of Processes Provide Paging, Virtual Memory, Memory Protection Provide Paging, Virtual Memory, Memory Protection Each Process scheduled independently Each Process scheduled independently Inter-process Communication Inter-process Communication
Tiny CPU and OS Disclaimer Goals Goals Meant to teach C# and exercise libraries Meant to teach basic OS/CPU concepts Meant to simulate OS behaviors NOT GOALS 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 The CPU is a 32-bit machine all addresses and registers are 32-bits CPU CPU 10 registers 32-bits wide IP – Instruction Pointer SP – Stack Pointer OS OS 32 basic OpCodes Each OpCode takes 1 clock cycle
Basic Design Object mapping to actual OS/CPU pieces Program Instruction Collection Instruction Instruction Instruction Instruction Process createProcess() 6 r4, $0 ;move 0 into r4 new Instruction(raw) new Instruction(raw) Program File on Disk Process in the OS
Basic Design Object mapping to actual OS/CPU pieces CPU Operating System (at run-time) Scheduler Loop Running Processes Collection ProcessProcessProcessProcess Each Process scheduled independently Each Process scheduled independently Poorly behaved processes are terminated Memory is protected unless shared
CPU and OS Executed like this from command-line: Executed like this from command-line: C:> OS 2048 prog1.txt prog2.txt Many Configuration options Many Configuration options<configuration><appSettings> </appSettings></configuration>
Instruction Set Example Opcode#Format Sleep25Sleep 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. SetPriority26SetPriority r1; Set the priority of the current process to the value in register r1; See priorities discussion in Operating system design Exit27Exit. This opcode is executed by a process to exit and be unloaded. Another process or the idle process must now be scheduled. FreeMemory28FreeMemory r1; Free the memory allocated whose address is in r1. MapSharedMem29MapSharedMem r1, r2; Map the shared memory region identified by r1 and return the start address in r2. SignalEvent30SignalEvent r1; Signal the event indicated by the value in register r1. WaitEvent31WaitEvent r1; Wait for the event in register r1 to be triggered. This results in context-switches happening. Input32Input r1; read the next 32-bit value into register r1. MemoryClear33MemoryClear r1, r2; set the bytes starting at address r1 of length r2 bytes to zero.
Example Idle Loop 6r4, $0;move 0 into r4 26r4;lower priority TO 0 6r1, $20 ;move 20 into r1 11r1;print the number 20 6r2, $-19;back up the ip 19 13r2;loop (jump back 19) Like a “Tiny Assembly Language” Like a “Tiny Assembly Language” Instructions are fetched from memory by the CPU and routed to the OS object 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 Manipulating Registers Moving Memory, using the Stack Moving Memory, using the Stack Small Functions Small Functions Inter-process Communication Inter-process Communication
Tiny OS Memory “Physical Memory” just array of bytes “Physical Memory” just array of bytes internal static byte physicalMemory; Virtual Memory may be much larger! Virtual Memory may be much larger! But each Process has it’s own “view” Virtual Memory hidden in code via  Operator Overloading Virtual Memory hidden in code via  Operator Overloading // Copy the code in one byte at a time Process p = new Process(++idPool, memSize); uint index = 0; foreach (byte b in processCode) memoryMgr[p.PCB.pid, index++] = b;
Tiny OS Memory 123 0 255 1 456 2 456 0 001 1 310 2 PhysicalVirtualProcess 255 3 052 4 123 5 056 … 635 456 412 324 123 644 543 654 Of course, the OS can address more memory than it has physically. So, you might have 256 bytes of memory, but can address 1024!
All memory operations such as paging and swapping are hidden from the Process. Tiny OS Memory 123 0 255 1 456 2 456 0 001 1 310 2 PhysicalVirtualProcess 255 3 052 4 123 5 056 … 635 456 412 324 123 644 543 654
<xml/> pages on disk Tiny OS Memory with small physical memory and paging 123 0 255 1 456 2 456 0 001 1 310 2 PhysicalVirtualProcess 255 3 052 4 123 5 056 … 635 456 412 324 123 644 543 654 For the Process, everything stays the same, except now parts of virtual memory map to disk!
Major Elements (1 of 2) Typed Collections Typed Collections Generated by Tool unsafe code unsafe code Like “inline C” Delegates Delegates Object-Oriented Function Pointers Object Serialization Object Serialization Memory Pages serialized to disk
Major Elements (2 of 2) Custom Exceptions Custom Exceptions Specific Derived Classes for each purpose Regular Expressions Regular Expressions Simplifies Program Parsing IComparable IComparable Custom Sorting in Collections
Demo: Changing the OS Changing Configuration Options Changing Configuration Options Modifying Virtual Memory Swapping! Modifying Virtual Memory Swapping! Adjusting Memory Page Size Adjusting Memory Page Size Analyzing Statistics and Profiling Analyzing Statistics and Profiling
Summary Tiny CPU and OS Learn C# and.NET 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! What.NET can do will surprise you!
Additional Resources Get the Deep Technical Drill Down Get the Deep Technical Drill Down DEV366:.NET Framework Under the Hood Learn how to Architect an application 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 http://msdn.microsoft.com