forked from BUPT-OS/easy_lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.c
37 lines (30 loc) · 981 Bytes
/
demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "uthread.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static struct uthread *current_thread = NULL;
static struct uthread *main_thread = NULL;
extern void thread_switch(struct context *from, struct context *to);
void bug(){
printf("missing return address\n");
exit(-1);
}
void test(){
printf("hello,%d\n",1);
thread_switch(¤t_thread->context,&main_thread->context);
}
int main(){
long long sp;
main_thread = malloc(sizeof(struct uthread));
memset(main_thread,0,sizeof(struct uthread));
current_thread = malloc(sizeof(struct uthread));
memset(current_thread,0,sizeof(struct uthread));
current_thread->context.rip = (long long)test;
sp = ((long long)¤t_thread->stack + STACK_SIZE) & (~(long long)15);
sp -= 8;
*(long long*)sp = (long long)bug;
current_thread->context.rsp = sp;
thread_switch(&main_thread->context,¤t_thread->context);
printf("main\n");
return 0;
}