signal.c 17 KB

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