-#if defined(__arm__)
- arm_thread_state_t state;
- flavor = ARM_THREAD_STATE;
- count = ARM_THREAD_STATE_COUNT;
- push = 0;
-#elif defined(__i386__) || defined(__x86_64__)
- i386_thread_state_t state;
- flavor = i386_THREAD_STATE;
- count = i386_THREAD_STATE_COUNT;
- push = 5;
+ mach_vm_address_t data(stack + Stack_);
+ _krncall(mach_vm_write(task, data, reinterpret_cast<mach_vm_address_t>(baton), depth));
+
+ mach_vm_address_t code;
+ _krncall(mach_vm_allocate(task, &code, trampoline->size_, true));
+ _krncall(mach_vm_write(task, code, reinterpret_cast<vm_offset_t>(trampoline->data_), trampoline->size_));
+ _krncall(mach_vm_protect(task, code, trampoline->size_, false, VM_PROT_READ | VM_PROT_EXECUTE));
+
+ uint32_t frame[push];
+ if (sizeof(frame) != 0)
+ memset(frame, 0, sizeof(frame));
+
+#if defined(__i386__) || defined(__x86_64__)
+ switch (state.tsh.flavor) {
+ case i386_THREAD_STATE:
+ frame[1] = data;
+ state.uts.ts32.__eip = code + trampoline->entry_;
+ state.uts.ts32.__esp = stack + Stack_ - sizeof(frame);
+ break;
+ case x86_THREAD_STATE64:
+ state.uts.ts64.__rdi = data;
+ state.uts.ts64.__rip = code + trampoline->entry_;
+ state.uts.ts64.__rsp = stack + Stack_ - sizeof(frame);
+ break;
+ default:
+ _assert(false);
+ }
+#elif defined(__arm__) || defined(__arm64__)
+ switch (state.ash.flavor) {
+ case ARM_THREAD_STATE32:
+ state.ts_32.__r[0] = data;
+ state.ts_32.__pc = code + trampoline->entry_;
+ state.ts_32.__sp = stack + Stack_ - sizeof(frame);
+
+ if ((state.ts_32.__pc & 0x1) != 0) {
+ state.ts_32.__pc &= ~0x1;
+ state.ts_32.__cpsr |= 0x20;
+ }
+
+ break;
+
+ case ARM_THREAD_STATE64:
+ state.ts_64.__x[0] = data;
+ state.ts_64.__pc = code + trampoline->entry_;
+ state.ts_64.__sp = stack + Stack_ - sizeof(frame);
+ break;
+
+ default:
+ _assert(false);
+ }