I am experimenting with the concept of swapping the stack to custom allocated memory in C (for changing the stack pointer and preserving registers, I am obviously using assembly). It is experiment on Linux x86_64.
Swapping is quite easy and works fine. I can call all functions from my files with no problems, and everything runs smoothly. Essentially, when the program starts, I push all registers to the stack, and then write the new stack pointer to the rsp register.
My stack after swapping looks somewhat like this, depending on what functions I call:
#2 0x0000555555555634 in unblock_signals () at stack_swap.c:53
#3 0x00005555555552ed in test1 () at kernel.c:36
#4 0x0000000000100000 in ?? ()
#5 0x0000000000000000 in ?? ()
However, after I change my stack, any syscall I make ends up with a Segmentation Fault. GDB reports that the problem is that after changing stack I cannot find system files responsible for those operations: like printf, or changing signal masks.
For example:
43 ./nptl/pthread_sigmask.c: No such file or directory.
I wonder why this happens. If program running main stack can access them, then what should I do to preserve that after changing stack pointer.
I was thinking to try unwind the original stack and copy everything from there to the new stack? But I am not sure if that is correct approach as I will have to populate with same information few different stacks.
What I am aiming to do is achieve a somewhat RTOS-like behavior—running a timer in the background and swapping between different tasks with different stacks. This is purely an educational experiment.
Hence, I would like to have few tasks running and freely executing function calls, and have posix timer in background calling function for changing between tasks (swapping their stacks).