signal.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017/10/5 Bernard the first version
  9. * 2018/09/17 Jesven fix: in _signal_deliver RT_THREAD_STAT_MASK to RT_THREAD_STAT_SIGNAL_MASK
  10. * 2018/11/22 Jesven in smp version rt_hw_context_switch_to add a param
  11. */
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #ifdef RT_USING_SIGNALS
  17. #ifndef RT_SIG_INFO_MAX
  18. #define RT_SIG_INFO_MAX 32
  19. #endif /* RT_SIG_INFO_MAX */
  20. #define DBG_TAG "SIGN"
  21. #define DBG_LVL DBG_WARNING
  22. #include <rtdbg.h>
  23. #define sig_mask(sig_no) (1u << sig_no)
  24. #define sig_valid(sig_no) (sig_no >= 0 && sig_no < RT_SIG_MAX)
  25. static struct rt_spinlock _thread_signal_lock = RT_SPINLOCK_INIT;
  26. struct siginfo_node
  27. {
  28. siginfo_t si;
  29. struct rt_slist_node list;
  30. };
  31. static struct rt_mempool *_siginfo_pool;
  32. static void _signal_deliver(rt_thread_t tid);
  33. void rt_thread_handle_sig(rt_bool_t clean_state);
  34. static void _signal_default_handler(int signo)
  35. {
  36. RT_UNUSED(signo);
  37. LOG_I("handled signo[%d] with default action.", signo);
  38. return ;
  39. }
  40. static void _signal_entry(void *parameter)
  41. {
  42. RT_UNUSED(parameter);
  43. rt_thread_t tid = rt_thread_self();
  44. /* handle signal */
  45. rt_thread_handle_sig(RT_FALSE);
  46. #ifdef RT_USING_SMP
  47. #else
  48. /* return to thread */
  49. tid->sp = tid->sig_ret;
  50. tid->sig_ret = RT_NULL;
  51. #endif /* RT_USING_SMP */
  52. LOG_D("switch back to: 0x%08x\n", tid->sp);
  53. RT_SCHED_CTX(tid).stat &= ~RT_THREAD_STAT_SIGNAL;
  54. #ifdef RT_USING_SMP
  55. rt_hw_context_switch_to((rt_base_t)&parameter, tid);
  56. #else
  57. rt_hw_context_switch_to((rt_ubase_t)&(tid->sp));
  58. #endif /* RT_USING_SMP */
  59. }
  60. /*
  61. * To deliver a signal to thread, there are cases:
  62. * 1. When thread is suspended, function resumes thread and
  63. * set signal stat;
  64. * 2. When thread is ready:
  65. * - If function delivers a signal to self thread, just handle
  66. * it.
  67. * - If function delivers a signal to another ready thread, OS
  68. * should build a slice context to handle it.
  69. */
  70. static void _signal_deliver(rt_thread_t tid)
  71. {
  72. rt_base_t level;
  73. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  74. /* thread is not interested in pended signals */
  75. if (!(tid->sig_pending & tid->sig_mask))
  76. {
  77. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  78. return;
  79. }
  80. if ((RT_SCHED_CTX(tid).stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK)
  81. {
  82. /* resume thread to handle signal */
  83. #ifdef RT_USING_SMART
  84. rt_thread_wakeup(tid);
  85. #else
  86. rt_thread_resume(tid);
  87. #endif
  88. /* add signal state */
  89. RT_SCHED_CTX(tid).stat |= (RT_THREAD_STAT_SIGNAL | RT_THREAD_STAT_SIGNAL_PENDING);
  90. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  91. /* re-schedule */
  92. rt_schedule();
  93. }
  94. else
  95. {
  96. if (tid == rt_thread_self())
  97. {
  98. /* add signal state */
  99. RT_SCHED_CTX(tid).stat |= RT_THREAD_STAT_SIGNAL;
  100. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  101. /* do signal action in self thread context */
  102. if (rt_interrupt_get_nest() == 0)
  103. {
  104. rt_thread_handle_sig(RT_TRUE);
  105. }
  106. }
  107. else if (!((RT_SCHED_CTX(tid).stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL))
  108. {
  109. /* add signal state */
  110. RT_SCHED_CTX(tid).stat |= (RT_THREAD_STAT_SIGNAL | RT_THREAD_STAT_SIGNAL_PENDING);
  111. #ifdef RT_USING_SMP
  112. {
  113. int cpu_id;
  114. cpu_id = RT_SCHED_CTX(tid).oncpu;
  115. if ((cpu_id != RT_CPU_DETACHED) && (cpu_id != rt_hw_cpu_id()))
  116. {
  117. rt_uint32_t cpu_mask;
  118. cpu_mask = RT_CPU_MASK ^ (1 << cpu_id);
  119. rt_hw_ipi_send(RT_SCHEDULE_IPI, cpu_mask);
  120. }
  121. }
  122. #else
  123. /* point to the signal handle entry */
  124. RT_SCHED_CTX(tid).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
  125. tid->sig_ret = tid->sp;
  126. tid->sp = rt_hw_stack_init((void *)_signal_entry, RT_NULL,
  127. (void *)((char *)tid->sig_ret - 32), RT_NULL);
  128. #endif /* RT_USING_SMP */
  129. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  130. LOG_D("signal stack pointer @ 0x%08x", tid->sp);
  131. /* re-schedule */
  132. rt_schedule();
  133. }
  134. else
  135. {
  136. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  137. }
  138. }
  139. }
  140. #ifdef RT_USING_SMP
  141. void *rt_signal_check(void* context)
  142. {
  143. rt_sched_lock_level_t level;
  144. int cpu_id;
  145. struct rt_cpu* pcpu;
  146. struct rt_thread *current_thread;
  147. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  148. cpu_id = rt_hw_cpu_id();
  149. pcpu = rt_cpu_index(cpu_id);
  150. current_thread = pcpu->current_thread;
  151. if (pcpu->irq_nest)
  152. {
  153. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  154. return context;
  155. }
  156. if (current_thread->cpus_lock_nest == 1)
  157. {
  158. if (RT_SCHED_CTX(current_thread).stat & RT_THREAD_STAT_SIGNAL_PENDING)
  159. {
  160. void *sig_context;
  161. RT_SCHED_CTX(current_thread).stat &= ~RT_THREAD_STAT_SIGNAL_PENDING;
  162. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  163. sig_context = rt_hw_stack_init((void *)_signal_entry, context,
  164. (void*)((char*)context - 32), RT_NULL);
  165. return sig_context;
  166. }
  167. }
  168. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  169. return context;
  170. }
  171. #endif /* RT_USING_SMP */
  172. /**
  173. * @brief This function will install a processing function to a specific
  174. * signal and return the old processing function of this signal.
  175. *
  176. * @note This function needs to be used in conjunction with the
  177. * rt_signal_unmask() function to make the signal effective.
  178. *
  179. * @see rt_signal_unmask()
  180. *
  181. * @param signo is a specific signal value (range: 0 ~ RT_SIG_MAX).
  182. *
  183. * @param handler is sets the processing of signal value.
  184. *
  185. * @return Return the old processing function of this signal. ONLY When the
  186. * return value is SIG_ERR, the operation is failed.
  187. */
  188. rt_sighandler_t rt_signal_install(int signo, rt_sighandler_t handler)
  189. {
  190. rt_base_t level;
  191. rt_sighandler_t old = RT_NULL;
  192. rt_thread_t tid = rt_thread_self();
  193. if (!sig_valid(signo)) return SIG_ERR;
  194. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  195. if (tid->sig_vectors == RT_NULL)
  196. {
  197. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  198. rt_thread_alloc_sig(tid);
  199. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  200. }
  201. if (tid->sig_vectors)
  202. {
  203. old = tid->sig_vectors[signo];
  204. if (handler == SIG_IGN) tid->sig_vectors[signo] = RT_NULL;
  205. else if (handler == SIG_DFL) tid->sig_vectors[signo] = _signal_default_handler;
  206. else tid->sig_vectors[signo] = handler;
  207. }
  208. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  209. return old;
  210. }
  211. /**
  212. * @brief This function will block the specified signal.
  213. *
  214. * @note This function will block the specified signal, even if the
  215. * rt_thread_kill() function is called to send this signal to
  216. * the current thread, it will no longer take effect.
  217. *
  218. * @see rt_thread_kill()
  219. *
  220. * @param signo is a specific signal value (range: 0 ~ RT_SIG_MAX).
  221. */
  222. void rt_signal_mask(int signo)
  223. {
  224. rt_base_t level;
  225. rt_thread_t tid = rt_thread_self();
  226. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  227. tid->sig_mask &= ~sig_mask(signo);
  228. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  229. }
  230. /**
  231. * @brief This function will unblock the specified signal.
  232. *
  233. * @note This function will unblock the specified signal. After calling
  234. * the rt_thread_kill() function to send this signal to the current
  235. * thread, it will take effect.
  236. *
  237. * @see rt_thread_kill()
  238. *
  239. * @param signo is a specific signal value (range: 0 ~ RT_SIG_MAX).
  240. */
  241. void rt_signal_unmask(int signo)
  242. {
  243. rt_base_t level;
  244. rt_thread_t tid = rt_thread_self();
  245. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  246. tid->sig_mask |= sig_mask(signo);
  247. /* let thread handle pended signals */
  248. if (tid->sig_mask & tid->sig_pending)
  249. {
  250. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  251. _signal_deliver(tid);
  252. }
  253. else
  254. {
  255. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  256. }
  257. }
  258. /**
  259. * @brief This function will wait for the arrival of the set signal. If it does not wait for this signal, the thread will be
  260. * suspended until it waits for this signal or the waiting time exceeds the specified timeout: timeout.
  261. *
  262. * @param set is the set of signal values to be waited for. Use the function
  263. * sigaddset() to add the signal.
  264. *
  265. * @param si is a pointer to the received signal info. If you don't care about this value, you can use RT_NULL to set.
  266. *
  267. * @param timeout is a timeout period (unit: an OS tick).
  268. *
  269. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  270. * If the return value is any other values, it means that the signal wait failed.
  271. */
  272. int rt_signal_wait(const rt_sigset_t *set, rt_siginfo_t *si, rt_int32_t timeout)
  273. {
  274. int ret = RT_EOK;
  275. rt_base_t level;
  276. rt_thread_t tid = rt_thread_self();
  277. struct siginfo_node *si_node = RT_NULL, *si_prev = RT_NULL;
  278. /* current context checking */
  279. RT_DEBUG_IN_THREAD_CONTEXT;
  280. /* parameters check */
  281. if (set == NULL || *set == 0 || si == NULL )
  282. {
  283. ret = -RT_EINVAL;
  284. goto __done_return;
  285. }
  286. /* clear siginfo to avoid unknown value */
  287. memset(si, 0x0, sizeof(rt_siginfo_t));
  288. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  289. /* already pending */
  290. if (tid->sig_pending & *set) goto __done;
  291. if (timeout == 0)
  292. {
  293. ret = -RT_ETIMEOUT;
  294. goto __done_int;
  295. }
  296. /* suspend self thread */
  297. rt_thread_suspend_with_flag(tid, RT_UNINTERRUPTIBLE);
  298. /* set thread stat as waiting for signal */
  299. RT_SCHED_CTX(tid).stat |= RT_THREAD_STAT_SIGNAL_WAIT;
  300. /* start timeout timer */
  301. if (timeout != RT_WAITING_FOREVER)
  302. {
  303. /* reset the timeout of thread timer and start it */
  304. rt_timer_control(&(tid->thread_timer),
  305. RT_TIMER_CTRL_SET_TIME,
  306. &timeout);
  307. rt_timer_start(&(tid->thread_timer));
  308. }
  309. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  310. /* do thread scheduling */
  311. rt_schedule();
  312. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  313. /* remove signal waiting flag */
  314. RT_SCHED_CTX(tid).stat &= ~RT_THREAD_STAT_SIGNAL_WAIT;
  315. /* check errno of thread */
  316. if (tid->error == -RT_ETIMEOUT)
  317. {
  318. tid->error = RT_EOK;
  319. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  320. /* timer timeout */
  321. ret = -RT_ETIMEOUT;
  322. goto __done_return;
  323. }
  324. __done:
  325. /* to get the first matched pending signals */
  326. si_node = (struct siginfo_node *)tid->si_list;
  327. while (si_node)
  328. {
  329. int signo;
  330. signo = si_node->si.si_signo;
  331. if (sig_mask(signo) & *set)
  332. {
  333. *si = si_node->si;
  334. LOG_D("sigwait: %d sig raised!", signo);
  335. if (si_prev) si_prev->list.next = si_node->list.next;
  336. else
  337. {
  338. struct siginfo_node *node_next;
  339. if (si_node->list.next)
  340. {
  341. node_next = (void *)rt_slist_entry(si_node->list.next, struct siginfo_node, list);
  342. tid->si_list = node_next;
  343. }
  344. else
  345. {
  346. tid->si_list = RT_NULL;
  347. }
  348. }
  349. /* clear pending */
  350. tid->sig_pending &= ~sig_mask(signo);
  351. rt_mp_free(si_node);
  352. break;
  353. }
  354. si_prev = si_node;
  355. if (si_node->list.next)
  356. {
  357. si_node = (void *)rt_slist_entry(si_node->list.next, struct siginfo_node, list);
  358. }
  359. else
  360. {
  361. si_node = RT_NULL;
  362. }
  363. }
  364. __done_int:
  365. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  366. __done_return:
  367. return ret;
  368. }
  369. void rt_thread_handle_sig(rt_bool_t clean_state)
  370. {
  371. rt_base_t level;
  372. rt_thread_t tid = rt_thread_self();
  373. struct siginfo_node *si_node;
  374. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  375. if (tid->sig_pending & tid->sig_mask)
  376. {
  377. /* if thread is not waiting for signal */
  378. if (!(RT_SCHED_CTX(tid).stat & RT_THREAD_STAT_SIGNAL_WAIT))
  379. {
  380. while (tid->sig_pending & tid->sig_mask)
  381. {
  382. int signo, error;
  383. rt_sighandler_t handler;
  384. si_node = (struct siginfo_node *)tid->si_list;
  385. if (!si_node) break;
  386. /* remove this sig info node from list */
  387. if (si_node->list.next == RT_NULL)
  388. tid->si_list = RT_NULL;
  389. else
  390. tid->si_list = (void *)rt_slist_entry(si_node->list.next, struct siginfo_node, list);
  391. signo = si_node->si.si_signo;
  392. handler = tid->sig_vectors[signo];
  393. tid->sig_pending &= ~sig_mask(signo);
  394. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  395. LOG_D("handle signal: %d, handler 0x%08x", signo, handler);
  396. if (handler) handler(signo);
  397. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  398. error = -RT_EINTR;
  399. rt_mp_free(si_node); /* release this siginfo node */
  400. /* set errno in thread tcb */
  401. tid->error = error;
  402. }
  403. /* whether clean signal status */
  404. if (clean_state == RT_TRUE)
  405. {
  406. RT_SCHED_CTX(tid).stat &= ~RT_THREAD_STAT_SIGNAL;
  407. }
  408. else
  409. {
  410. return;
  411. }
  412. }
  413. }
  414. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  415. }
  416. void rt_thread_alloc_sig(rt_thread_t tid)
  417. {
  418. int index;
  419. rt_bool_t need_free = RT_FALSE;
  420. rt_base_t level;
  421. rt_sighandler_t *vectors;
  422. vectors = (rt_sighandler_t *)RT_KERNEL_MALLOC(sizeof(rt_sighandler_t) * RT_SIG_MAX);
  423. RT_ASSERT(vectors != RT_NULL);
  424. for (index = 0; index < RT_SIG_MAX; index ++)
  425. {
  426. vectors[index] = _signal_default_handler;
  427. }
  428. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  429. if (tid->sig_vectors == RT_NULL)
  430. {
  431. tid->sig_vectors = vectors;
  432. }
  433. else
  434. {
  435. need_free = RT_TRUE;
  436. }
  437. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  438. if (need_free)
  439. {
  440. rt_free(vectors);
  441. }
  442. }
  443. void rt_thread_free_sig(rt_thread_t tid)
  444. {
  445. rt_base_t level;
  446. struct siginfo_node *si_node;
  447. rt_sighandler_t *sig_vectors;
  448. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  449. si_node = (struct siginfo_node *)tid->si_list;
  450. tid->si_list = RT_NULL;
  451. sig_vectors = tid->sig_vectors;
  452. tid->sig_vectors = RT_NULL;
  453. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  454. if (si_node)
  455. {
  456. struct rt_slist_node *node;
  457. struct rt_slist_node *node_to_free;
  458. LOG_D("free signal info list");
  459. node = &(si_node->list);
  460. do
  461. {
  462. node_to_free = node;
  463. node = node->next;
  464. si_node = rt_slist_entry(node_to_free, struct siginfo_node, list);
  465. rt_mp_free(si_node);
  466. } while (node);
  467. }
  468. if (sig_vectors)
  469. {
  470. RT_KERNEL_FREE(sig_vectors);
  471. }
  472. }
  473. /**
  474. * @brief This function can be used to send any signal to any thread.
  475. *
  476. * @param tid is a pointer to the thread that receives the signal.
  477. *
  478. * @param sig is a specific signal value (range: 0 ~ RT_SIG_MAX).
  479. *
  480. * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
  481. * If the return value is any other values, it means that the signal send failed.
  482. */
  483. int rt_thread_kill(rt_thread_t tid, int sig)
  484. {
  485. siginfo_t si;
  486. rt_base_t level;
  487. struct siginfo_node *si_node;
  488. RT_ASSERT(tid != RT_NULL);
  489. if (!sig_valid(sig)) return -RT_EINVAL;
  490. LOG_I("send signal: %d", sig);
  491. si.si_signo = sig;
  492. si.si_code = SI_USER;
  493. si.si_value.sival_ptr = RT_NULL;
  494. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  495. if (tid->sig_pending & sig_mask(sig))
  496. {
  497. /* whether already emits this signal? */
  498. struct rt_slist_node *node;
  499. struct siginfo_node *entry;
  500. si_node = (struct siginfo_node *)tid->si_list;
  501. if (si_node)
  502. node = (struct rt_slist_node *)&si_node->list;
  503. else
  504. node = RT_NULL;
  505. /* update sig info */
  506. for (; (node) != RT_NULL; node = node->next)
  507. {
  508. entry = rt_slist_entry(node, struct siginfo_node, list);
  509. if (entry->si.si_signo == sig)
  510. {
  511. memcpy(&(entry->si), &si, sizeof(siginfo_t));
  512. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  513. return 0;
  514. }
  515. }
  516. }
  517. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  518. si_node = (struct siginfo_node *) rt_mp_alloc(_siginfo_pool, 0);
  519. if (si_node)
  520. {
  521. rt_slist_init(&(si_node->list));
  522. memcpy(&(si_node->si), &si, sizeof(siginfo_t));
  523. level = rt_spin_lock_irqsave(&_thread_signal_lock);
  524. if (tid->si_list)
  525. {
  526. struct siginfo_node *si_list;
  527. si_list = (struct siginfo_node *)tid->si_list;
  528. rt_slist_append(&(si_list->list), &(si_node->list));
  529. }
  530. else
  531. {
  532. tid->si_list = si_node;
  533. }
  534. /* a new signal */
  535. tid->sig_pending |= sig_mask(sig);
  536. rt_spin_unlock_irqrestore(&_thread_signal_lock, level);
  537. }
  538. else
  539. {
  540. LOG_E("The allocation of signal info node failed.");
  541. }
  542. /* deliver signal to this thread */
  543. _signal_deliver(tid);
  544. return RT_EOK;
  545. }
  546. int rt_system_signal_init(void)
  547. {
  548. _siginfo_pool = rt_mp_create("signal", RT_SIG_INFO_MAX, sizeof(struct siginfo_node));
  549. if (_siginfo_pool == RT_NULL)
  550. {
  551. LOG_E("create memory pool for signal info failed.");
  552. RT_ASSERT(0);
  553. }
  554. return 0;
  555. }
  556. #endif /* RT_USING_SIGNALS */