os-workshop

same materials and sample source for RV32 OS projects
git clone http://frotz.net/git/os-workshop.git
Log | Files | Refs

context-switch.S (1119B)


      1 // Copyright 2022, Brian Swetland <swetland@frotz.net>
      2 // Licensed under the Apache License, Version 2.0
      3 
      4 // this code must match the structure rv32_ctxt_t in context-switch.h
      5 
      6 // void context_switch(cframe_t* from, cframe_t* to)
      7 .globl context_switch
      8 context_switch:
      9 	// save old context to 'from'
     10 	sw ra, 0x00(a0)
     11 	sw sp, 0x04(a0)
     12 	sw gp, 0x08(a0)
     13 	sw tp, 0x0C(a0)
     14 	sw s0, 0x10(a0)
     15 	sw s1, 0x14(a0)
     16 	sw s2, 0x18(a0)
     17 	sw s3, 0x1C(a0)
     18 	sw s4, 0x20(a0)
     19 	sw s5, 0x24(a0)
     20 	sw s6, 0x28(a0)
     21 	sw s7, 0x2C(a0)
     22 	sw s8, 0x30(a0)
     23 	sw s9, 0x34(a0)
     24 	sw s10, 0x38(a0)
     25 	sw s11, 0x3C(a0)
     26 
     27         // load new context from 'to'
     28 	lw ra, 0x00(a1)
     29 	lw sp, 0x04(a1)
     30 	lw gp, 0x08(a1)
     31 	lw tp, 0x0C(a1)
     32 	lw s0, 0x10(a1)
     33 	lw s1, 0x14(a1)
     34 	lw s2, 0x18(a1)
     35 	lw s3, 0x1C(a1)
     36 	lw s4, 0x20(a1)
     37 	lw s5, 0x24(a1)
     38 	lw s6, 0x28(a1)
     39 	lw s7, 0x2C(a1)
     40 	lw s8, 0x30(a1)
     41 	lw s9, 0x34(a1)
     42 	lw s10, 0x38(a1)
     43 	lw s11, 0x3C(a1)
     44 
     45 	// return to new context
     46 	ret
     47 
     48 // startup helper
     49 // calls routine in s2 with s0, s1 as arguments
     50 // then calls thread_exit() if that routine returns
     51 .globl context_entry
     52 context_entry:
     53 	mv a0, s0
     54 	mv a1, s1
     55 	jalr s2
     56 	j thread_exit