I am trying to understand some basics of the OS using some tasks. I have already posted a similar question and received satisfactory answers. But it is a little different, but I could not debug it. So here is what I do:
What I want to do is run the main program, malloc space, use it as a stack to start a user level thread. My problem is with the return address. Here's the code for now:
[I am editing my code so that it is updated to the current state of my response]
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define STACK_SIZE 512
void switch_thread(int*,int*);
int k = 0;
void simple_function()
{
printf("I am the function! k is: %d\n",k);
exit(0);
}
void create_thread(void (*function)())
{
int* stack = malloc(STACK_SIZE + 32);
stack = (int* )(((long)stack & (-1 << 4)) + 0x10);
stack = (int* ) ((long)stack + STACK_SIZE);
*stack = (long) function;
switch_thread(stack,stack);
}
int main()
{
create_thread(simple_function);
assert(0);
return 0;
}
switch_thread is the assembly code that I wrote as follows:
.text
.globl switch_thread
switch_thread:
movq %rdi, %rsp
movq %rsi, %rbp
ret
GDB ( simple_function " - ! k is: 0". . .
. .