1f10521ccSWarner Losh /*
2f10521ccSWarner Losh * arm thread support
3f10521ccSWarner Losh *
4f10521ccSWarner Losh * Copyright (c) 2013 Stacey D. Son
5f10521ccSWarner Losh *
6f10521ccSWarner Losh * This program is free software; you can redistribute it and/or modify
7f10521ccSWarner Losh * it under the terms of the GNU General Public License as published by
8f10521ccSWarner Losh * the Free Software Foundation; either version 2 of the License, or
9f10521ccSWarner Losh * (at your option) any later version.
10f10521ccSWarner Losh *
11f10521ccSWarner Losh * This program is distributed in the hope that it will be useful,
12f10521ccSWarner Losh * but WITHOUT ANY WARRANTY; without even the implied warranty of
13f10521ccSWarner Losh * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14f10521ccSWarner Losh * GNU General Public License for more details.
15f10521ccSWarner Losh *
16f10521ccSWarner Losh * You should have received a copy of the GNU General Public License
17f10521ccSWarner Losh * along with this program; if not, see <http://www.gnu.org/licenses/>.
18f10521ccSWarner Losh */
19*9c092804SMarkus Armbruster
20*9c092804SMarkus Armbruster #ifndef TARGET_ARCH_THREAD_H
21*9c092804SMarkus Armbruster #define TARGET_ARCH_THREAD_H
22f10521ccSWarner Losh
23f10521ccSWarner Losh /* Compare to arm/arm/vm_machdep.c cpu_set_upcall_kse() */
target_thread_set_upcall(CPUARMState * env,abi_ulong entry,abi_ulong arg,abi_ulong stack_base,abi_ulong stack_size)24f10521ccSWarner Losh static inline void target_thread_set_upcall(CPUARMState *env, abi_ulong entry,
25f10521ccSWarner Losh abi_ulong arg, abi_ulong stack_base, abi_ulong stack_size)
26f10521ccSWarner Losh {
27f10521ccSWarner Losh abi_ulong sp;
28f10521ccSWarner Losh
29f10521ccSWarner Losh /*
30f10521ccSWarner Losh * Make sure the stack is properly aligned.
31f10521ccSWarner Losh * arm/include/param.h (STACKLIGN() macro)
32f10521ccSWarner Losh */
33f10521ccSWarner Losh sp = (u_int)(stack_base + stack_size) & ~0x7;
34f10521ccSWarner Losh
35f10521ccSWarner Losh /* sp = stack base */
36f10521ccSWarner Losh env->regs[13] = sp;
37f10521ccSWarner Losh /* pc = start function entry */
38f10521ccSWarner Losh env->regs[15] = entry & 0xfffffffe;
39f10521ccSWarner Losh /* r0 = arg */
40f10521ccSWarner Losh env->regs[0] = arg;
41f10521ccSWarner Losh env->spsr = ARM_CPU_MODE_USR;
42f10521ccSWarner Losh /*
43f10521ccSWarner Losh * Thumb mode is encoded by the low bit in the entry point (since ARM can't
44f10521ccSWarner Losh * execute at odd addresses). When it's set, set the Thumb bit (T) in the
45f10521ccSWarner Losh * CPSR.
46f10521ccSWarner Losh */
47f10521ccSWarner Losh cpsr_write(env, (entry & 1) * CPSR_T, CPSR_T, CPSRWriteByInstr);
48f10521ccSWarner Losh }
49f10521ccSWarner Losh
target_thread_init(struct target_pt_regs * regs,struct image_info * infop)50f10521ccSWarner Losh static inline void target_thread_init(struct target_pt_regs *regs,
51f10521ccSWarner Losh struct image_info *infop)
52f10521ccSWarner Losh {
53f10521ccSWarner Losh abi_long stack = infop->start_stack;
54f10521ccSWarner Losh memset(regs, 0, sizeof(*regs));
55f10521ccSWarner Losh regs->ARM_cpsr = ARM_CPU_MODE_USR;
56f10521ccSWarner Losh /*
57f10521ccSWarner Losh * Thumb mode is encoded by the low bit in the entry point (since ARM can't
58f10521ccSWarner Losh * execute at odd addresses). When it's set, set the Thumb bit (T) in the
59f10521ccSWarner Losh * CPSR.
60f10521ccSWarner Losh */
61f10521ccSWarner Losh if (infop->entry & 1) {
62f10521ccSWarner Losh regs->ARM_cpsr |= CPSR_T;
63f10521ccSWarner Losh }
64f10521ccSWarner Losh regs->ARM_pc = infop->entry & 0xfffffffe;
65f10521ccSWarner Losh regs->ARM_sp = stack;
66f10521ccSWarner Losh regs->ARM_lr = infop->entry & 0xfffffffe;
67f10521ccSWarner Losh /*
68f10521ccSWarner Losh * FreeBSD kernel passes the ps_strings pointer in r0. This is used by some
69f10521ccSWarner Losh * programs to set status messages that we see in ps. bsd-user doesn't
70f10521ccSWarner Losh * support that functionality, so it's ignored. When set to 0, FreeBSD's csu
71f10521ccSWarner Losh * code ignores it. For the static case, r1 and r2 are effectively ignored
72f10521ccSWarner Losh * by the csu __startup() routine. For the dynamic case, rtld saves r0 but
73f10521ccSWarner Losh * generates r1 and r2 and passes them into the csu _startup.
74f10521ccSWarner Losh *
75f10521ccSWarner Losh * r0 ps_strings 0 passed since ps arg setting not supported
76f10521ccSWarner Losh * r1 obj_main ignored by _start(), so 0 passed
77f10521ccSWarner Losh * r2 cleanup generated by rtld or ignored by _start(), so 0 passed
78f10521ccSWarner Losh */
79f10521ccSWarner Losh }
80f10521ccSWarner Losh
81*9c092804SMarkus Armbruster #endif /* TARGET_ARCH_THREAD_H */
82