From c6a60f113001beff924fb5449cbb358d3b03ad8d Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Tue, 17 Dec 2024 23:50:34 +0100 Subject: xpost: add figures to match threads --- content/2023-01-14-xpost-matcha-threads/gen-svg.sh | 2 + content/2023-01-14-xpost-matcha-threads/index.md | 33 +++- .../init-stack.drawio | 25 +++ .../2023-01-14-xpost-matcha-threads/init-stack.svg | 3 + .../2023-01-14-xpost-matcha-threads/yield.drawio | 170 +++++++++++++++++++++ content/2023-01-14-xpost-matcha-threads/yield.svg | 3 + 6 files changed, 228 insertions(+), 8 deletions(-) create mode 100644 content/2023-01-14-xpost-matcha-threads/init-stack.drawio create mode 100644 content/2023-01-14-xpost-matcha-threads/init-stack.svg create mode 100644 content/2023-01-14-xpost-matcha-threads/yield.drawio create mode 100644 content/2023-01-14-xpost-matcha-threads/yield.svg (limited to 'content') diff --git a/content/2023-01-14-xpost-matcha-threads/gen-svg.sh b/content/2023-01-14-xpost-matcha-threads/gen-svg.sh index 365e243..8991440 100644 --- a/content/2023-01-14-xpost-matcha-threads/gen-svg.sh +++ b/content/2023-01-14-xpost-matcha-threads/gen-svg.sh @@ -1,3 +1,5 @@ #!/bin/bash drawio -x -f svg -t --svg-theme dark -o os-vs-user-threads.svg os-vs-user-threads.drawio +drawio -x -f svg -t --svg-theme dark -o yield.svg yield.drawio +drawio -x -f svg -t --svg-theme dark -o init-stack.svg init-stack.drawio diff --git a/content/2023-01-14-xpost-matcha-threads/index.md b/content/2023-01-14-xpost-matcha-threads/index.md index 3456b09..a916145 100644 --- a/content/2023-01-14-xpost-matcha-threads/index.md +++ b/content/2023-01-14-xpost-matcha-threads/index.md @@ -51,14 +51,11 @@ underlying cooperative-multitasking and implement such a `yield()` function as shown in the example above. Looking at the final implementation, the yield function does the following: -```txt -yield: - 1. function prologue - 2. push callee-saved regs to current stack - 3. swap stack pointers (current - new) - 4. pop callee-saved regs from new stack - 5. function epilogue & return -``` +1. push callee-saved regs to current stack +2. swap stack pointers (current - new) +3. pop callee-saved regs from new stack + + Implementations for different ISAs are available here: - [x86_64][yield-x86] @@ -66,6 +63,24 @@ Implementations for different ISAs are available here: - [armv7a][yield-arm] - [riscv64][yield-rv64] +
+ + +Since a thread returns into the last stack-frame of the new thread after +switching the stack pointers in the yield function, special care must be taken +when a new stack is created. + +The _initial stack_ is setup such that, when yield-ing into the new stack for +the first time, the stack contains the initial values for the callee-saved +registers, which yield will restore and the return frame contains an address +which should be returned to when returning from yield. + +An example of setting up the initial stack can be seen in [init_stack +(x86)][init-stack]. From this it can also be seen that the first time the +thread will return to [thread_create][thread-create], which just calls into a +function passed to [init_stack][init-stack]. +
+ ## Appendix: os-level vs user-level threading The figure below depicts *os-level* threading (left) vs *user-level* threading @@ -89,3 +104,5 @@ to the scheduler, the stack-pointer is switched back to **stack S**. [yield-arm]: https://github.com/johannst/matcha-threads/blob/master/lib/arch/arm/yield.s [yield-arm64]: https://github.com/johannst/matcha-threads/blob/master/lib/arch/arm64/yield.s [yield-rv64]: https://github.com/johannst/matcha-threads/blob/master/lib/arch/riscv64/yield.s +[init-stack]: https://github.com/johannst/matcha-threads/blob/master/lib/arch/x86_64/init_stack.cc +[thread-create]: https://github.com/johannst/matcha-threads/blob/master/lib/arch/x86_64/thread_create.s diff --git a/content/2023-01-14-xpost-matcha-threads/init-stack.drawio b/content/2023-01-14-xpost-matcha-threads/init-stack.drawio new file mode 100644 index 0000000..eb0859f --- /dev/null +++ b/content/2023-01-14-xpost-matcha-threads/init-stack.drawio @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/content/2023-01-14-xpost-matcha-threads/init-stack.svg b/content/2023-01-14-xpost-matcha-threads/init-stack.svg new file mode 100644 index 0000000..2abc191 --- /dev/null +++ b/content/2023-01-14-xpost-matcha-threads/init-stack.svg @@ -0,0 +1,3 @@ + + +
initial
stack layout
initial...
initial values for callee-saved registers
initial values for c...
return frame
(thread_create)
return frame...
thread_create args
thread_create args
Text is not SVG - cannot display
\ No newline at end of file diff --git a/content/2023-01-14-xpost-matcha-threads/yield.drawio b/content/2023-01-14-xpost-matcha-threads/yield.drawio new file mode 100644 index 0000000..164a1f1 --- /dev/null +++ b/content/2023-01-14-xpost-matcha-threads/yield.drawio @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/content/2023-01-14-xpost-matcha-threads/yield.svg b/content/2023-01-14-xpost-matcha-threads/yield.svg new file mode 100644 index 0000000..508a163 --- /dev/null +++ b/content/2023-01-14-xpost-matcha-threads/yield.svg @@ -0,0 +1,3 @@ + + +
process address space
process address space
stack A
stack A
stack B
stack B
core
core
sp
sp
callee-saved regs
callee-saved regs
callee-saved regs
callee-saved regs
process address space
process address space
stack A
stack A
stack B
stack B
callee-saved regs
callee-saved regs
regs
regs
callee-saved regs
callee-saved regs
process address space
process address space
stack A
stack A
stack B
stack B
callee-saved regs
callee-saved regs
(1) save callee-saved registers of current thread
(1) save callee-saved r...
(2) move to new stack pointer
(2) move to new stack p...
(3) restore callee-saved registers of new thread
(3) restore callee-save...
core
core
sp
sp
regs
regs
core
core
sp
sp
regs
regs
Text is not SVG - cannot display
\ No newline at end of file -- cgit v1.2.3