-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
making one function to use local variabe of other function.
- Loading branch information
0 parents
commit af21184
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
all: context_asm.S ctx_switch.c | ||
gcc -c context_asm.S -o context_asm.o -ggdb3 | ||
gcc -c ctx_switch.c -o ctx_switch.o -ggdb3 | ||
gcc ctx_switch.o context_asm.o -o run -ggdb3 | ||
|
||
clean: | ||
rm -f context_asm.o ctx_switch.o run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Can one function access the local variable of other? No, right? | ||
Look the code and figure out how function "funa" is using local variable "i" of funb. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
.extern cur_ctx | ||
.globl my_yeild | ||
|
||
|
||
#ifdef __x86_64__ | ||
|
||
my_yeild: | ||
movq cur_ctx, %rsi | ||
movq %rsp, 0(%rsi) | ||
/* Save the callee saved registers */ | ||
movq %r12, 8(%rsi) | ||
movq %r13, 16(%rsi) | ||
movq %r14, 24(%rsi) | ||
movq %r15, 32(%rsi) | ||
|
||
my_set: | ||
movq %rdi, cur_ctx /*argument will be in rdi */ | ||
/* reload the registers */ | ||
movq 0(%rdi), %rsp | ||
movq 8(%rdi), %r12 | ||
movq 16(%rdi), %r13 | ||
movq 24(%rdi), %r14 | ||
movq 32(%rdi), %r15 | ||
ret | ||
|
||
#endif | ||
|
||
#ifdef __i386__ | ||
|
||
my_yeild: | ||
movl cur_ctx, %eax | ||
movl %esp, 0(%eax) | ||
/* Save the callee saved registers */ | ||
movl %ebx, 4(%eax) | ||
movl %esi, 8(%eax) | ||
movl %edi, 12(%eax) | ||
|
||
my_set: | ||
movl 4(%esp), %eax /* agrument will be on stack*/ | ||
movl %eax, cur_ctx | ||
/* reload the registers */ | ||
movl 0(%eax), %esp | ||
movl 4(%eax), %ebx | ||
movl 8(%eax), %esi | ||
movl 12(%eax), %edi | ||
ret | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
typedef size_t uintptr_t; | ||
|
||
#ifdef __x86_64__ | ||
#define NUM_REG 4 | ||
#elif __i386__ | ||
#define NUM_REG 3 | ||
#else | ||
#define NUM_REG 0 | ||
#endif | ||
|
||
struct stack_context { | ||
uintptr_t sp_reg; | ||
uintptr_t other_reg[NUM_REG]; | ||
}actx, bctx, pctx; | ||
|
||
struct stack_context *cur_ctx; | ||
#define STACK_SIZE 4096 | ||
|
||
unsigned int astack[STACK_SIZE]; | ||
unsigned int bstack[STACK_SIZE]; | ||
|
||
extern void my_yeild(struct stack_context *rctx); | ||
extern void funa(void); | ||
extern void funb(void); | ||
|
||
void funa(void) | ||
{ | ||
int i = 0; | ||
my_yeild(&bctx); | ||
while (i++ < 20) { | ||
printf("a %d\n", i); | ||
sleep(1); | ||
my_yeild(&bctx); | ||
} | ||
exit(0); | ||
} | ||
|
||
void funb(void) | ||
{ | ||
int i = 0; | ||
while (i++ < 20) { | ||
printf("b %d\n", i); | ||
sleep(1); | ||
my_yeild(&actx); | ||
} | ||
exit(0); | ||
} | ||
|
||
int main() | ||
{ | ||
cur_ctx = &pctx; | ||
uintptr_t *sp = (uintptr_t *) ((((uintptr_t)&astack[STACK_SIZE]) & -16L) - sizeof(uintptr_t)); | ||
*sp = (uintptr_t)funa; | ||
actx.sp_reg = (uintptr_t)sp; | ||
|
||
sp = (uintptr_t *) ((((uintptr_t)&bstack[STACK_SIZE]) & -16L) - sizeof(uintptr_t)); | ||
*sp = (uintptr_t)funb; | ||
bctx.sp_reg = (uintptr_t) sp; | ||
my_yeild(&actx); | ||
return 0; | ||
} |