Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ARM buildin setjmp/longjmp support #3

Merged
merged 2 commits into from
Sep 3, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions md.S
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,39 @@
#elif defined(__arm__)

/* TODO: FIXME: implement setjmp and longjmp for ARM */
.globl _st_md_cxt_save
.type _st_md_cxt_save, %function
.align 2
_st_md_cxt_save:
mov ip, r0 // r0为参数jmpbuf地址
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// r0 is the param jmpbuf ptr address.

// 以下指令等同于
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save registers like:

// *ip++ = v1
// *ip++ = ...
// *ip++ = v6
// *ip++ = sl
// *ip++ = fp
stmia ip!, {v1-v6, sl, fp} // TODO:根据不同的ARM版本,这里要做不同的寄存器保存
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// TODO: compatible with other ARM version.

movs r2, sp
stmia ip!, {r2, lr}
mov r0, #0 // setjmp first return 0(r0存放返回值)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// r0 save the return value(0) of setjmp.

bx lr // return
.size _st_md_cxt_save, .-_st_md_cxt_save

.globl _st_md_cxt_restore
.type _st_md_cxt_restore, %function
.align 2
_st_md_cxt_restore:
mov ip, r0 // r0 -> jmp_buf
movs r0, r1 // r1 -> return value
// 下面两条指令是一组,也就是if(r0 == 0) r0 = 1
Copy link
Member

@winlinvip winlinvip Sep 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// The bellow is a group, that is:
// if (r0 == 0) r0 =1;

ITT eq
moveq r0, #1 // 从long_jmp返回一定不能是0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// long_jmp should never return 0


ldmia ip!, {v1-v6, sl, fp} // 恢复寄存器
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// restore registers.

ldr sp, [ip], #4 // ip会+4, 这条指令相当于sp = *ip; ip+=4;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// restore sp, like: sp=*ip; ip+=4;

ldr lr, [ip], #4
bx lr
.size _st_md_cxt_restore, .-_st_md_cxt_restore

#endif

12 changes: 6 additions & 6 deletions md.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,12 +421,12 @@
#elif defined(__arm__)
#define MD_STACK_GROWS_DOWN

#if defined(__GLIBC__) && __GLIBC__ >= 2
/* Merge from https://github.com/michaeltalyansky/state-threads/commit/56554a5c425aee8e7a73782eae23d74d83c4120a */
#define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[8]
#else
#error "ARM/Linux pre-glibc2 not supported yet"
#endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
#define MD_USE_BUILTIN_SETJMP

#ifndef JB_RSP
#define JB_RSP 8 // 这里的JB_RSP的具体数值要跟md.S里面的保存位置对应
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个按照ST的惯例,我觉得还是不要改了,就魔法数吧,没有关系的。
只保留宏定义:MD_USE_BUILTIN_SETJMP

#endif
#define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[JB_RSP]

#elif defined(__s390__)
#define MD_STACK_GROWS_DOWN
Expand Down