MCPcopy Create free account
hub / github.com/apache/nuttx / arm_fork

Function arm_fork

arch/arm/src/common/arm_fork.c:92–245  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

90 ****************************************************************************/
91
92pid_t arm_fork(const struct fork_s *context)
93{
94 struct tcb_s *parent = this_task();
95 struct tcb_s *child;
96 uint32_t newsp;
97 uint32_t newfp;
98 uint32_t newtop;
99 uint32_t stacktop;
100 uint32_t stackutil;
101#ifdef CONFIG_ARCH_KERNEL_STACK
102 uint32_t oldsp = (uint32_t)parent->xcp.ustkptr;
103#else
104 uint32_t oldsp = context->sp;
105#endif
106
107 sinfo("fork context [%p]:\n", context);
108 sinfo(" r4:%08" PRIx32 " r5:%08" PRIx32
109 " r6:%08" PRIx32 " r7:%08" PRIx32 "\n",
110 context->r4, context->r5, context->r6, context->r7);
111 sinfo(" r8:%08" PRIx32 " r9:%08" PRIx32 " r10:%08" PRIx32 "\n",
112 context->r8, context->r9, context->r10);
113 sinfo(" r11:%08" PRIx32 " sp:%08" PRIx32 " lr:%08" PRIx32 "\n",
114 context->r11, oldsp, context->lr);
115
116 /* Allocate and initialize a TCB for the child task. */
117
118 child = nxtask_setup_fork((start_t)(context->lr & ~1));
119 if (!child)
120 {
121 serr("ERROR: nxtask_setup_fork failed\n");
122 return (pid_t)ERROR;
123 }
124
125 sinfo("TCBs: Parent=%p Child=%p\n", parent, child);
126
127 /* How much of the parent's stack was utilized? The ARM uses
128 * a push-down stack so that the current stack pointer should
129 * be lower than the initial, adjusted stack pointer. The
130 * stack usage should be the difference between those two.
131 */
132
133 stacktop = (uint32_t)parent->stack_base_ptr +
134 parent->adj_stack_size;
135 DEBUGASSERT(stacktop > oldsp && oldsp >= (uint32_t)parent->stack_base_ptr);
136 stackutil = stacktop - oldsp;
137
138 sinfo("Parent: stackutil:%" PRIu32 "\n", stackutil);
139
140 /* Make some feeble effort to preserve the stack contents. This is
141 * feeble because the stack surely contains invalid pointers and other
142 * content that will not work in the child context. However, if the
143 * user follows all of the caveats of fork() usage, even this feeble
144 * effort is overkill.
145 */
146
147 newtop = (uint32_t)child->stack_base_ptr +
148 child->adj_stack_size;
149

Callers

nothing calls this directly

Calls 4

this_taskFunction · 0.85
nxtask_setup_forkFunction · 0.85
nxtask_start_forkFunction · 0.85
memcpyFunction · 0.50

Tested by

no test coverage detected