Kennem's Blog
  • 🏠主页
  • 🔍搜索
  • 📚文章
  • ⏱时间轴
  • 🔖标签
  • 🗂️分类
  • 🙋🏻‍♂️关于
主页 🗂️ 分类

操作系统

MIT6.S081(13)-Coordination (sleep&wakeup)

MIT6.S081(13)-Coordination (sleep&wakeup) plan Re-emphasize a few points about xv6 thread switching sequence coordination sleep & wakeup lost wakeup problem termination Why hold p->lock across swtch()? this is an important point and affects many situations in xv6 [diagram: P1, STACK1, swtch, STACK_SCHED] yield: acquire(&p->lock); p->state = RUNNABLE; swtch(); scheduler: swtch(); release(&p->lock); the main point of holding p->lock across swtch(): prevent another core’s scheduler from seeing p->state == RUNNABLE until after the original core has stopped executing the thread and after the original core has stopped using the thread’s stack ...

2024-09-29 · 12 分钟 · 5649 字 · ShowGuan

MIT6.S081(11)- Thread switching

MIT6.S081(11)- Thread switching Topic: more “under the hood” with xv6 Previously: system calls, interrupts, page tables, locks Today: process/thread switching Why support multiple tasks? Time-sharing: many users and/or many running programs. program structure: prime number sieve. parallel speedup on multi-core hardware. Threads are an abstraction to simplify programming when there are many tasks. thread = an independent serial execution – registers, pc, stack the threading system interleaves the execution of multiple threads ...

2024-09-21 · 7 分钟 · 3091 字 · ShowGuan

MIT6.S081(10)-Locking

MIT6.S081(10)-Locking Why talk about locking? apps want to use multi-core processors for parallel speed-up so kernel must deal with parallel system calls and thus parallel access to kernel data (buffer cache, processes, &c) locks help with correct sharing of data locks can limit parallel speedup What goes wrong if we don’t have locks Case study: delete acquire/release in kalloc.c Boot kernel works! Run usertests all tests pass! except we lose some pages Why do we lose pages? picture of shared-memory multiprocessor race between two cores calling kfree() BUMMER: we need locks for correctness but lose performance (kfree is serialized) ...

2024-09-19 · 5 分钟 · 2151 字 · ShowGuan

MIT6.S081(9)-Interrupts

MIT6.S081(9)-Interrupts 6.S081 2020 Lecture 9: Interrupts Interrupts hardware wants attention now! e.g., pkt arrived, clock interrupt software must set aside current work and respond on RISC-V use same trap mechanism as for syscalls and exceptions new issues/complications: asynchronous interrupts running process interrupt handler may not run in context of process who caused interrupt concurrency devices and process run in parallel programming devices device can be difficult to program Where do device interrupts come from? diagram: Fig 1 of SiFive board in FU540-C000-v1.0.pdf CPUs, CLINT, PLIC, devices Fig 3 has more detail for interrupts Section 8 and 9 of FU540-C000-v1.0.pdf UART (universal asynchronous receiver/transmitter) See Section 13 of FU540-C000-v1.0.pdf Although the QEMU version is slightly different Follows http://byterunner.com/16550.html the interrupt tells the kernel the device hardware wants attention the driver (in the kernel) knows how to tell the device to do things often the interrupt handler calls the relevant driver but other arrangements are possible (schedule a thread; poll) [diagram: top-half/bottom-half] ...

2024-09-18 · 3 分钟 · 1303 字 · ShowGuan

MIT6.S081(8)-Page faults

MIT6.S081(8)-Page faults plan: cool things you can do with vm Better performance/efficiency e.g., one zero-filled page e.g., copy-on-write fork New features e.g., memory-mapped files virtual memory: several views primary purpose: isolation each process has its own address space Virtual memory provides a level-of-indirection provides kernel with opportunity to do cool stuff already some examples: shared trampoline page guard page but more possible… Key idea: change page tables on page fault Page fault is a form of a trap (like a system call) Xv6 panics on page fault But you don’t have to panic! Instead: update page table instead of panic restart instruction (see userret() from traps lecture) Combination of page faults and updating page table is powerful! RISC-V page faults 3 of 16 exceptions are related to paging Exceptions cause controlled transfers to kernel See traps lecture Information we might need at page fault to do something interesting: The virtual address that caused the fault See stval register; page faults set it to the fault address The type of violation that caused the fault See scause register value (instruction, load, and Store page fault) The instruction and mode where the fault occurred User IP: tf->epc U/K mode: implicit in usertrap/kerneltrap lazy/on-demand page allocation sbrk() is old fashioned; applications often ask for memory they need for example, the allocate for the largest possible input but an application will typically use less if they ask for much, sbrk() could be expensive for example, if all memory is in use, have to wait until kernel has evicted some pages to free up memory sbrk allocates memory that may never be used. moderns OSes allocate memory lazily plan: allocate physical memory when application needs it adjust p->sz on sbrk, but don’t allocate when application uses that memory, it will result in page fault on pagefault allocate memory resume at the fault instruction may use less memory if not used, no fault, no allocation spreads the cost of allocation over the page faults instead of upfront in sbrk() demo modify sysproc.c modify trap.c modify vm.c one zero-filled page (zero fill on demand) applications often have large part of memory that must zero global arrays, etc. the “block starting symbol” (bbs) segment thus, kernel must often fill a page with zeros idea: memset one page with zeros map that page copy-on-write when kernel needs zero-filled page on write make copy of page and map it read/write in app address space copy-on-write fork observation: xv6 fork copies all pages from parent (see fork()) but fork is often immediately followed by exec idea: share address space between parent and child modify fork() to map pages copy-on-write use extra available system bits (RSW) in PTEs on page fault, make copy of page and map it read/write demand paging observation: exec loads the complete file into memory (see exec.c) expensive: takes time to do so (e.g., file is stored on a slow disk) unnecessary: maybe not the whole file will be used idea: load pages from the file on demand allocate page table entries, but mark them on-demand on fault, read the page in from the file and update page table entry need to keep some meta information about where a page is located on disk this information is typically in structure called virtual memory area (VMA) challenge: file larger than physical memory (see next idea) use virtual memory larger than physical memory observation: application may need more memory than there is physical memory idea: store less-frequently used parts of the address space on disk page-in and page-out pages of the address address space transparently works when working sets fits in physical memory most popular replacement strategy: least-recently used (LRU) the A(cess) bit in the PTE helps the kernel implementing LRU demo: run top and vmstat on laptop and dialup.athena.mit.edu see VIRT RES MEM SHR columns memory-mapped files idea: allow access to files using load and store can easily read and writes part of a file e.g., don’t have to change offset using lseek system call Unix systems a new system call for m-mapped files: void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); kernel page-in pages of a file on demand when memory is full, page-out pages of a file that are not frequently used shared virtual memory idea: allow processes on different machines to share virtual memory gives the illusion of physical shared memory, across a network replicate pages that are only read invalidate copies on write TLB management CPUs caches paging translation for speed xv6 flushes entire TLB during user/kernel transitions why? RISC-V allows more sophisticated plans PTE_G: global TLB bits what page could use this? ASID numbers TLB entries are tagged with ASID, so kernel can flush selectively SATP takes an ASID number sfence.vma also takes an ASID number Large pages 2MB and 1GB Virtual memory is still evolving Recent changes in Linux PKTI to handle meltdown side-channel (https://en.wikipedia.org/wiki/Kernel_page-table_isolation) xv6 basically implements KPTI Somewhat recent changes Support for 5-level page tables (57 address bits!) Support for ASIDs Less recent changes Support for large pages NX (No eXecute) PTE_X flag ...

2024-09-17 · 8 分钟 · 3585 字 · ShowGuan

MIT6.S081(7)-Q&A

MIT6.S081(7)-Q&A Plan: answering your questions Approach: walk through staff solutions start with pgtbl lab because it was the hardest your questions are at bottom of this file Pgtbl lab comments few lines of code, but difficult-to-debug bugs worst case: qemu/xv6 stops running “best” case: kernel panic hard to debug for staff too there are so many possible reasons why you discovered once we hadn’t seen yet likely to be the most challenging lab historically the first VM lab is hard this year too, even though we made a new lab to provide a gentler intro to VM Part 1 of pgtbl lab Explain vm output in terms of fig 3-4 ...

2024-09-12 · 6 分钟 · 2628 字 · ShowGuan

MIT6.S081(6)-System Call Entry/Exit

MIT6.S081(6)-System Call Entry/Exit Today: user -> kernel transition system calls, faults, interrupts enter the kernel in the same way important for isolation and performance lots of careful design and important detail What needs to happen when a program makes a system call, e.g. write()? [CPU | user/kernel diagram] CPU resources are set up for user execution (not kernel) 32 registers, sp, pc, privilege mode, satp, stvec, sepc, … what needs to happen? save 32 user registers and pc switch to supervisor mode switch to kernel page table switch to kernel stack jump to kernel C code high-level goals don’t let user code interfere with user->kernel transition e.g. don’t execute user code in supervisor mode! transparent to user code – resume without disturbing ...

2024-09-10 · 7 分钟 · 3444 字 · ShowGuan

MIT6.S081(5)-RISC-V calling convention, stack frames, and gdb

MIT6.S081(5)-RISC-V calling convention, stack frames, and gdb C code is compiled to machine instructions. How does the machine work at a lower level? How does this translation work? How to interact between C and asm Why this matters: sometimes need to write code not expressible in C And you need this for the syscall lab! RISC-V abstract machine No C-like control flow, no concept of variables, types … Base ISA: Program counter, 32 general-purpose registers (x0–x31) ...

2024-09-04 · 5 分钟 · 2064 字 · ShowGuan

C 语言指针

C 语言指针 C语言中的内存 静态内存(Static Memory) Global variables, accessible throughout the whole program. Defined with static keyword, as well as variables defined in global scope. 栈内存(Stack Memory) Local variables with functions. Destroyed after function exits. 堆内存(Heap Memory) ...

2024-09-03 · 7 分钟 · 3405 字 · ShowGuan

MIT6.S081(4)-Virtual Memory

MIT6.S081(4)-Virtual Memory Plan: Address spaces Paging hardware xv6 VM code Virtual memory overview Today’s problem: [user/kernel diagram] [memory view: diagram with user processes and kernel in memory] Suppose the shell has a bug: sometimes it writes to a random memory address how can we keep it from wrecking the kernel? and from wrecking other processes? we want isolated address spaces each process has its own memory it can read and write its own memory it cannot read or write anything else ...

2024-09-02 · 12 分钟 · 5742 字 · ShowGuan

MIT6.S081(1)-O/S overview

MIT6.S081(1)-O/S overview Class Page Overview 6.S081 goals Understand operating system (O/S) design and implementation Hands-on experience extending a small O/S Hands-on experience writing systems software What is the purpose of an O/S? Abstract the hardware for convenience and portability Multiplex the hardware among many applications Isolate applications in order to contain bugs Allow sharing among cooperating applications Control sharing for security Don’t get in the way of high performance Support a wide range of applications Organization: layered picture user applications: vi, gcc, DB, &c kernel services h/w: CPU, RAM, disk, net, &c we care a lot about the interfaces and internal kernel structure ...

2024-08-31 · 12 分钟 · 5896 字 · ShowGuan

MIT6.S081(3)-OS organization

MIT6.S081(3)-OS organization Lecture Topic: OS design ​ system calls ​ micro/monolithic kernel First system call in xv6 OS picture apps: sh, echo, … system call interface (open, close,…) OS Goal of OS run multiple applications isolate them multiplex them share Strawman design: No OS Application directly interacts with hardware CPU cores & registers DRAM chips Disk blocks … OS library perhaps abstracts some of it Strawman design not conducive to multiplexing each app periodically must give up hardware BUT, weak isolation app forgets to give up, no other app runs apps has end-less loop, no other app runs you cannot even kill the badly app from another app but used by real-time OSes “cooperative scheduling” ...

2024-08-31 · 4 分钟 · 1800 字 · ShowGuan
© 2026 Kennem's Blog · Powered by Hugo & PaperMod
👤 Visitors: 👀 Views: