1
1
/*
2
- * Copyright (c) 2006-2021, RT-Thread Development Team
2
+ * Copyright (c) 2006-2025 RT-Thread Development Team
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*
60
60
extern char working_directory [];
61
61
#endif
62
62
63
+ /**
64
+ * @brief Initializes the LWP (Light-Weight Process) component
65
+ *
66
+ * @return int Returns RT_EOK if all initializations succeed, otherwise returns
67
+ * the error code from the first failed initialization
68
+ *
69
+ * @note This function performs initialization of various LWP subsystems in sequence:
70
+ * 1. Thread ID (TID) initialization
71
+ * 2. Process ID (PID) initialization
72
+ * 3. Channel component initialization
73
+ * 4. Futex (Fast Userspace Mutex) initialization
74
+ */
63
75
static int lwp_component_init (void )
64
76
{
65
77
int rc ;
@@ -83,6 +95,15 @@ static int lwp_component_init(void)
83
95
}
84
96
INIT_COMPONENT_EXPORT (lwp_component_init );
85
97
98
+ /**
99
+ * @brief Sets the current working directory for the calling LWP or system
100
+ *
101
+ * @param[in] buf Pointer to the path string to set as working directory
102
+ *
103
+ * @note This function handles both LWP-specific and system-wide working directories:
104
+ * - For LWPs, sets the working_directory in the LWP structure
105
+ * - For non-LWP threads, sets the global working_directory variable
106
+ */
86
107
void lwp_setcwd (char * buf )
87
108
{
88
109
struct rt_lwp * lwp = RT_NULL ;
@@ -106,6 +127,15 @@ void lwp_setcwd(char *buf)
106
127
return ;
107
128
}
108
129
130
+ /**
131
+ * @brief Get the current working directory for the light-weight process
132
+ *
133
+ * @return char* Pointer to the current working directory string
134
+ *
135
+ * @note The function returns either:
136
+ * - LWP's working directory (if valid and absolute path)
137
+ * - System default working directory (if no LWP or invalid path)
138
+ */
109
139
char * lwp_getcwd (void )
110
140
{
111
141
char * dir_buf = RT_NULL ;
@@ -135,13 +165,28 @@ char *lwp_getcwd(void)
135
165
}
136
166
137
167
/**
138
- * RT-Thread light-weight process
168
+ * @brief Set the kernel stack pointer for the current thread
169
+ *
170
+ * @param[in] sp Pointer to the new kernel stack location
171
+ *
172
+ * @note It's typically used during context switching or thread initialization.
139
173
*/
140
174
void lwp_set_kernel_sp (uint32_t * sp )
141
175
{
142
176
rt_thread_self ()-> kernel_sp = (rt_uint32_t * )sp ;
143
177
}
144
178
179
+ /**
180
+ * @brief Get the kernel stack pointer for the current thread
181
+ *
182
+ * @return uint32_t* Pointer to the kernel stack
183
+ *
184
+ * @note Architecture-specific behavior:
185
+ * 1. With MMU: Simply returns the current thread's stack pointer
186
+ * 2. Without MMU: Checks interrupt context and returns either:
187
+ * - Interrupted thread's kernel_sp (if in interrupt)
188
+ * - Current thread's kernel_sp (if not in interrupt)
189
+ */
145
190
uint32_t * lwp_get_kernel_sp (void )
146
191
{
147
192
#ifdef ARCH_MM_MMU
@@ -162,8 +207,15 @@ uint32_t *lwp_get_kernel_sp(void)
162
207
#endif
163
208
}
164
209
165
-
166
- /* lwp-thread clean up routine */
210
+ /**
211
+ * @brief Clean up resources associated with a light-weight process thread
212
+ *
213
+ * @param[in] tid Pointer to the thread control block to be cleaned up
214
+ *
215
+ * @note This function performs cleanup operations for a thread associated with a light-weight process (LWP).
216
+ * It handles signal detachment and reference count decrement for the LWP structure.
217
+ *
218
+ */
167
219
void lwp_cleanup (struct rt_thread * tid )
168
220
{
169
221
struct rt_lwp * lwp ;
@@ -191,6 +243,15 @@ void lwp_cleanup(struct rt_thread *tid)
191
243
return ;
192
244
}
193
245
246
+ /**
247
+ * @brief Set up standard I/O for a light-weight process
248
+ *
249
+ * @param[in] lwp Pointer to the light-weight process structure
250
+ *
251
+ * @note This function initializes the standard input, output, and error streams
252
+ * for a light-weight process by opening the console device and associating
253
+ * it with file descriptors 0, 1, and 2.
254
+ */
194
255
static void lwp_execve_setup_stdio (struct rt_lwp * lwp )
195
256
{
196
257
struct dfs_fdtable * lwp_fdt ;
@@ -223,6 +284,14 @@ static void lwp_execve_setup_stdio(struct rt_lwp *lwp)
223
284
return ;
224
285
}
225
286
287
+ /**
288
+ * @brief Entry point for light-weight process threads
289
+ *
290
+ * @param[in] parameter Thread parameter (unused)
291
+ *
292
+ * @note This function is the main entry point for threads created within a light-weight process.
293
+ * It handles thread initialization, debug mode setup, and transitions to user mode.
294
+ */
226
295
static void _lwp_thread_entry (void * parameter )
227
296
{
228
297
rt_thread_t tid ;
@@ -262,6 +331,15 @@ static void _lwp_thread_entry(void *parameter)
262
331
#endif /* ARCH_MM_MMU */
263
332
}
264
333
334
+ /**
335
+ * @brief Get the current light-weight process
336
+ *
337
+ * @return Pointer to the current light-weight process structure
338
+ * RT_NULL if no process is associated with current thread
339
+ *
340
+ * @note This function retrieves the light-weight process associated with the
341
+ * currently running thread.
342
+ */
265
343
struct rt_lwp * lwp_self (void )
266
344
{
267
345
rt_thread_t tid ;
@@ -275,6 +353,17 @@ struct rt_lwp *lwp_self(void)
275
353
return RT_NULL ;
276
354
}
277
355
356
+ /**
357
+ * @brief Register a child process with its parent
358
+ *
359
+ * @param[in] parent Pointer to the parent process structure
360
+ * @param[in] child Pointer to the child process structure to register
361
+ *
362
+ * @return RT_EOK on success
363
+ *
364
+ * @note This function adds a child process to its parent's children list and
365
+ * increases reference counts for both processes.
366
+ */
278
367
rt_err_t lwp_children_register (struct rt_lwp * parent , struct rt_lwp * child )
279
368
{
280
369
/* lwp add to children link */
@@ -293,6 +382,17 @@ rt_err_t lwp_children_register(struct rt_lwp *parent, struct rt_lwp *child)
293
382
return 0 ;
294
383
}
295
384
385
+ /**
386
+ * @brief Unregister a child process from its parent
387
+ *
388
+ * @param[in] parent Pointer to the parent process structure
389
+ * @param[in] child Pointer to the child process structure to unregister
390
+ *
391
+ * @return RT_EOK on success
392
+ *
393
+ * @note This function removes a child process from its parent's children list and
394
+ * decreases reference counts for both processes.
395
+ */
296
396
rt_err_t lwp_children_unregister (struct rt_lwp * parent , struct rt_lwp * child )
297
397
{
298
398
struct rt_lwp * * lwp_node ;
@@ -316,6 +416,23 @@ rt_err_t lwp_children_unregister(struct rt_lwp *parent, struct rt_lwp *child)
316
416
return 0 ;
317
417
}
318
418
419
+ /**
420
+ * @brief Copy process arguments and environment variables from kernel space to user space.
421
+ *
422
+ * @param[in] lwp Pointer to the light-weight process structure
423
+ * @param[in] argc Argument count
424
+ * @param[in] argv Argument vector
425
+ * @param[in] envp Environment variables
426
+ *
427
+ * @return Pointer to the process auxiliary structure on success
428
+ * RT_NULL if memory allocation fails or arguments initialization fails
429
+ *
430
+ * @note This function performs the following operations:
431
+ * 1. Initializes argument information structure
432
+ * 2. Copies command line arguments to user space
433
+ * 3. Copies environment variables to user space
434
+ * 4. Returns the auxiliary structure containing copied data
435
+ */
319
436
struct process_aux * argscopy (struct rt_lwp * lwp , int argc , char * * argv , char * * envp )
320
437
{
321
438
struct lwp_args_info ai ;
@@ -344,6 +461,30 @@ struct process_aux *argscopy(struct rt_lwp *lwp, int argc, char **argv, char **e
344
461
return ua ;
345
462
}
346
463
464
+ /**
465
+ * @brief Creates and starts a new LWP by loading and executing the specified executable file.
466
+ *
467
+ * @param[in] filename Path to the executable file
468
+ * @param[in] debug Debug flag (non-zero to enable debugging)
469
+ * @param[in] argc Argument count
470
+ * @param[in] argv Argument vector
471
+ * @param[in] envp Environment variables
472
+ *
473
+ * @return Process ID (PID) of the new LWP on success
474
+ * -EINVAL if filename is NULL
475
+ * -EACCES if file is not executable
476
+ * -ENOMEM if memory allocation fails
477
+ * -RT_ERROR on other failures
478
+ *
479
+ * @note This function performs the following operations:
480
+ * 1. Validates input parameters
481
+ * 2. Creates new LWP structure
482
+ * 3. Initializes user space (for MMU systems)
483
+ * 4. Copies arguments and environment
484
+ * 5. Loads the executable
485
+ * 6. Sets up standard I/O
486
+ * 7. Creates and starts the main thread
487
+ */
347
488
pid_t lwp_execve (char * filename , int debug , int argc , char * * argv , char * * envp )
348
489
{
349
490
int result ;
@@ -499,13 +640,38 @@ extern char **__environ;
499
640
char * * __environ = 0 ;
500
641
#endif
501
642
643
+ /**
644
+ * @brief Execute a new program in the current process context
645
+ *
646
+ * @param[in] filename Path to the executable file
647
+ * @param[in] debug Debug flag (non-zero enables debug mode)
648
+ * @param[in] argc Number of command line arguments
649
+ * @param[in] argv Array of command line argument strings
650
+ *
651
+ * @return Process ID (PID) of the new process on success
652
+ * Negative error code on failure
653
+ *
654
+ * @note This is a wrapper function for lwp_execve.
655
+ *
656
+ * @see lwp_execve()
657
+ */
502
658
pid_t exec (char * filename , int debug , int argc , char * * argv )
503
659
{
504
660
setenv ("OS" , "RT-Thread" , 1 );
505
661
return lwp_execve (filename , debug , argc , argv , __environ );
506
662
}
507
663
508
664
#ifdef ARCH_MM_MMU
665
+ /**
666
+ * @brief Saves thread-specific user settings (TID register)
667
+ *
668
+ * @param[in,out] thread Pointer to the thread control block
669
+ *
670
+ * @note This function stores the architecture-specific TID register
671
+ * into the specified thread's control block.This is typically used
672
+ * when switching between threads to preserve thread-specific settings
673
+ */
674
+
509
675
void lwp_user_setting_save (rt_thread_t thread )
510
676
{
511
677
if (thread )
@@ -514,6 +680,14 @@ void lwp_user_setting_save(rt_thread_t thread)
514
680
}
515
681
}
516
682
683
+ /**
684
+ * @brief Restores thread-specific user settings (TID register and debug state)
685
+ *
686
+ * @param[in] thread Pointer to the thread control block
687
+ *
688
+ * @note This function restores architecture-specific Thread ID Register (TIDR) value
689
+ * and debug-related settings for the specified thread.
690
+ */
517
691
void lwp_user_setting_restore (rt_thread_t thread )
518
692
{
519
693
if (!thread )
@@ -556,20 +730,46 @@ void lwp_user_setting_restore(rt_thread_t thread)
556
730
}
557
731
#endif /* ARCH_MM_MMU */
558
732
733
+ /**
734
+ * @brief Saves user thread context pointer
735
+ *
736
+ * @param[in] ctx Pointer to user thread context structure to be saved
737
+ *
738
+ * @note This function stores a pointer to user thread context in the current thread's
739
+ * control block for later restoration. The context pointer is typically used
740
+ * during thread context switching.
741
+ */
559
742
void lwp_uthread_ctx_save (void * ctx )
560
743
{
561
744
rt_thread_t thread ;
562
745
thread = rt_thread_self ();
563
746
thread -> user_ctx .ctx = ctx ;
564
747
}
565
748
749
+ /**
750
+ * @brief Restores the user thread context by clearing the context pointer
751
+ *
752
+ * @note Typically called during thread context switching to clean up any
753
+ * previously saved user context.
754
+ */
566
755
void lwp_uthread_ctx_restore (void )
567
756
{
568
757
rt_thread_t thread ;
569
758
thread = rt_thread_self ();
570
759
thread -> user_ctx .ctx = RT_NULL ;
571
760
}
572
761
762
+ /**
763
+ * @brief Prints a backtrace of the current thread's call stack
764
+ *
765
+ * @param[in] uthread The thread to backtrace (must be associated with an LWP)
766
+ * @param[in] frame Pointer to the initial stack frame
767
+ *
768
+ * @return RT_EOK on success, -RT_ERROR on failure
769
+ *
770
+ * @note This function prints a backtrace of the call stack for the specified user thread,
771
+ * providing addresses that can be used with addr2line to get file and line information.
772
+ */
573
773
rt_err_t lwp_backtrace_frame (rt_thread_t uthread , struct rt_hw_backtrace_frame * frame )
574
774
{
575
775
rt_err_t rc = - RT_ERROR ;
0 commit comments