signal32.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* Signal support for 32-bit kernel builds
  2. *
  3. * Copyright (C) 2001 Matthew Wilcox <willy at parisc-linux.org>
  4. * Copyright (C) 2006 Kyle McMartin <kyle at parisc-linux.org>
  5. *
  6. * Code was mostly borrowed from kernel/signal.c.
  7. * See kernel/signal.c for additional Copyrights.
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/compat.h>
  25. #include <linux/module.h>
  26. #include <linux/unistd.h>
  27. #include <linux/init.h>
  28. #include <linux/sched.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/types.h>
  31. #include <linux/errno.h>
  32. #include <asm/uaccess.h>
  33. #include "signal32.h"
  34. #define DEBUG_COMPAT_SIG 0
  35. #define DEBUG_COMPAT_SIG_LEVEL 2
  36. #if DEBUG_COMPAT_SIG
  37. #define DBG(LEVEL, ...) \
  38. ((DEBUG_COMPAT_SIG_LEVEL >= LEVEL) \
  39. ? printk(__VA_ARGS__) : (void) 0)
  40. #else
  41. #define DBG(LEVEL, ...)
  42. #endif
  43. inline void
  44. sigset_32to64(sigset_t *s64, compat_sigset_t *s32)
  45. {
  46. s64->sig[0] = s32->sig[0] | ((unsigned long)s32->sig[1] << 32);
  47. }
  48. inline void
  49. sigset_64to32(compat_sigset_t *s32, sigset_t *s64)
  50. {
  51. s32->sig[0] = s64->sig[0] & 0xffffffffUL;
  52. s32->sig[1] = (s64->sig[0] >> 32) & 0xffffffffUL;
  53. }
  54. long
  55. restore_sigcontext32(struct compat_sigcontext __user *sc, struct compat_regfile __user * rf,
  56. struct pt_regs *regs)
  57. {
  58. long err = 0;
  59. compat_uint_t compat_reg;
  60. compat_uint_t compat_regt;
  61. int regn;
  62. /* When loading 32-bit values into 64-bit registers make
  63. sure to clear the upper 32-bits */
  64. DBG(2,"restore_sigcontext32: PER_LINUX32 process\n");
  65. DBG(2,"restore_sigcontext32: sc = 0x%p, rf = 0x%p, regs = 0x%p\n", sc, rf, regs);
  66. DBG(2,"restore_sigcontext32: compat_sigcontext is %#lx bytes\n", sizeof(*sc));
  67. for(regn=0; regn < 32; regn++){
  68. err |= __get_user(compat_reg,&sc->sc_gr[regn]);
  69. regs->gr[regn] = compat_reg;
  70. /* Load upper half */
  71. err |= __get_user(compat_regt,&rf->rf_gr[regn]);
  72. regs->gr[regn] = ((u64)compat_regt << 32) | (u64)compat_reg;
  73. DBG(3,"restore_sigcontext32: gr%02d = %#lx (%#x / %#x)\n",
  74. regn, regs->gr[regn], compat_regt, compat_reg);
  75. }
  76. DBG(2,"restore_sigcontext32: sc->sc_fr = 0x%p (%#lx)\n",sc->sc_fr, sizeof(sc->sc_fr));
  77. /* XXX: BE WARNED FR's are 64-BIT! */
  78. err |= __copy_from_user(regs->fr, sc->sc_fr, sizeof(regs->fr));
  79. /* Better safe than sorry, pass __get_user two things of
  80. the same size and let gcc do the upward conversion to
  81. 64-bits */
  82. err |= __get_user(compat_reg, &sc->sc_iaoq[0]);
  83. /* Load upper half */
  84. err |= __get_user(compat_regt, &rf->rf_iaoq[0]);
  85. regs->iaoq[0] = ((u64)compat_regt << 32) | (u64)compat_reg;
  86. DBG(2,"restore_sigcontext32: upper half of iaoq[0] = %#lx\n", compat_regt);
  87. DBG(2,"restore_sigcontext32: sc->sc_iaoq[0] = %p => %#x\n",
  88. &sc->sc_iaoq[0], compat_reg);
  89. err |= __get_user(compat_reg, &sc->sc_iaoq[1]);
  90. /* Load upper half */
  91. err |= __get_user(compat_regt, &rf->rf_iaoq[1]);
  92. regs->iaoq[1] = ((u64)compat_regt << 32) | (u64)compat_reg;
  93. DBG(2,"restore_sigcontext32: upper half of iaoq[1] = %#lx\n", compat_regt);
  94. DBG(2,"restore_sigcontext32: sc->sc_iaoq[1] = %p => %#x\n",
  95. &sc->sc_iaoq[1],compat_reg);
  96. DBG(2,"restore_sigcontext32: iaoq is %#lx / %#lx\n",
  97. regs->iaoq[0],regs->iaoq[1]);
  98. err |= __get_user(compat_reg, &sc->sc_iasq[0]);
  99. /* Load the upper half for iasq */
  100. err |= __get_user(compat_regt, &rf->rf_iasq[0]);
  101. regs->iasq[0] = ((u64)compat_regt << 32) | (u64)compat_reg;
  102. DBG(2,"restore_sigcontext32: upper half of iasq[0] = %#lx\n", compat_regt);
  103. err |= __get_user(compat_reg, &sc->sc_iasq[1]);
  104. /* Load the upper half for iasq */
  105. err |= __get_user(compat_regt, &rf->rf_iasq[1]);
  106. regs->iasq[1] = ((u64)compat_regt << 32) | (u64)compat_reg;
  107. DBG(2,"restore_sigcontext32: upper half of iasq[1] = %#lx\n", compat_regt);
  108. DBG(2,"restore_sigcontext32: iasq is %#lx / %#lx\n",
  109. regs->iasq[0],regs->iasq[1]);
  110. err |= __get_user(compat_reg, &sc->sc_sar);
  111. /* Load the upper half for sar */
  112. err |= __get_user(compat_regt, &rf->rf_sar);
  113. regs->sar = ((u64)compat_regt << 32) | (u64)compat_reg;
  114. DBG(2,"restore_sigcontext32: upper_half & sar = %#lx\n", compat_regt);
  115. DBG(2,"restore_sigcontext32: sar is %#lx\n", regs->sar);
  116. DBG(2,"restore_sigcontext32: r28 is %ld\n", regs->gr[28]);
  117. return err;
  118. }
  119. /*
  120. * Set up the sigcontext structure for this process.
  121. * This is not an easy task if the kernel is 64-bit, it will require
  122. * that we examine the process personality to determine if we need to
  123. * truncate for a 32-bit userspace.
  124. */
  125. long
  126. setup_sigcontext32(struct compat_sigcontext __user *sc, struct compat_regfile __user * rf,
  127. struct pt_regs *regs, int in_syscall)
  128. {
  129. compat_int_t flags = 0;
  130. long err = 0;
  131. compat_uint_t compat_reg;
  132. compat_uint_t compat_regb;
  133. int regn;
  134. if (on_sig_stack((unsigned long) sc))
  135. flags |= PARISC_SC_FLAG_ONSTACK;
  136. if (in_syscall) {
  137. DBG(1,"setup_sigcontext32: in_syscall\n");
  138. flags |= PARISC_SC_FLAG_IN_SYSCALL;
  139. /* Truncate gr31 */
  140. compat_reg = (compat_uint_t)(regs->gr[31]);
  141. /* regs->iaoq is undefined in the syscall return path */
  142. err |= __put_user(compat_reg, &sc->sc_iaoq[0]);
  143. DBG(2,"setup_sigcontext32: sc->sc_iaoq[0] = %p <= %#x\n",
  144. &sc->sc_iaoq[0], compat_reg);
  145. /* Store upper half */
  146. compat_reg = (compat_uint_t)(regs->gr[31] >> 32);
  147. err |= __put_user(compat_reg, &rf->rf_iaoq[0]);
  148. DBG(2,"setup_sigcontext32: upper half iaoq[0] = %#x\n", compat_reg);
  149. compat_reg = (compat_uint_t)(regs->gr[31]+4);
  150. err |= __put_user(compat_reg, &sc->sc_iaoq[1]);
  151. DBG(2,"setup_sigcontext32: sc->sc_iaoq[1] = %p <= %#x\n",
  152. &sc->sc_iaoq[1], compat_reg);
  153. /* Store upper half */
  154. compat_reg = (compat_uint_t)((regs->gr[31]+4) >> 32);
  155. err |= __put_user(compat_reg, &rf->rf_iaoq[1]);
  156. DBG(2,"setup_sigcontext32: upper half iaoq[1] = %#x\n", compat_reg);
  157. /* Truncate sr3 */
  158. compat_reg = (compat_uint_t)(regs->sr[3]);
  159. err |= __put_user(compat_reg, &sc->sc_iasq[0]);
  160. err |= __put_user(compat_reg, &sc->sc_iasq[1]);
  161. /* Store upper half */
  162. compat_reg = (compat_uint_t)(regs->sr[3] >> 32);
  163. err |= __put_user(compat_reg, &rf->rf_iasq[0]);
  164. err |= __put_user(compat_reg, &rf->rf_iasq[1]);
  165. DBG(2,"setup_sigcontext32: upper half iasq[0] = %#x\n", compat_reg);
  166. DBG(2,"setup_sigcontext32: upper half iasq[1] = %#x\n", compat_reg);
  167. DBG(1,"setup_sigcontext32: iaoq %#lx / %#lx\n",
  168. regs->gr[31], regs->gr[31]+4);
  169. } else {
  170. compat_reg = (compat_uint_t)(regs->iaoq[0]);
  171. err |= __put_user(compat_reg, &sc->sc_iaoq[0]);
  172. DBG(2,"setup_sigcontext32: sc->sc_iaoq[0] = %p <= %#x\n",
  173. &sc->sc_iaoq[0], compat_reg);
  174. /* Store upper half */
  175. compat_reg = (compat_uint_t)(regs->iaoq[0] >> 32);
  176. err |= __put_user(compat_reg, &rf->rf_iaoq[0]);
  177. DBG(2,"setup_sigcontext32: upper half iaoq[0] = %#x\n", compat_reg);
  178. compat_reg = (compat_uint_t)(regs->iaoq[1]);
  179. err |= __put_user(compat_reg, &sc->sc_iaoq[1]);
  180. DBG(2,"setup_sigcontext32: sc->sc_iaoq[1] = %p <= %#x\n",
  181. &sc->sc_iaoq[1], compat_reg);
  182. /* Store upper half */
  183. compat_reg = (compat_uint_t)(regs->iaoq[1] >> 32);
  184. err |= __put_user(compat_reg, &rf->rf_iaoq[1]);
  185. DBG(2,"setup_sigcontext32: upper half iaoq[1] = %#x\n", compat_reg);
  186. compat_reg = (compat_uint_t)(regs->iasq[0]);
  187. err |= __put_user(compat_reg, &sc->sc_iasq[0]);
  188. DBG(2,"setup_sigcontext32: sc->sc_iasq[0] = %p <= %#x\n",
  189. &sc->sc_iasq[0], compat_reg);
  190. /* Store upper half */
  191. compat_reg = (compat_uint_t)(regs->iasq[0] >> 32);
  192. err |= __put_user(compat_reg, &rf->rf_iasq[0]);
  193. DBG(2,"setup_sigcontext32: upper half iasq[0] = %#x\n", compat_reg);
  194. compat_reg = (compat_uint_t)(regs->iasq[1]);
  195. err |= __put_user(compat_reg, &sc->sc_iasq[1]);
  196. DBG(2,"setup_sigcontext32: sc->sc_iasq[1] = %p <= %#x\n",
  197. &sc->sc_iasq[1], compat_reg);
  198. /* Store upper half */
  199. compat_reg = (compat_uint_t)(regs->iasq[1] >> 32);
  200. err |= __put_user(compat_reg, &rf->rf_iasq[1]);
  201. DBG(2,"setup_sigcontext32: upper half iasq[1] = %#x\n", compat_reg);
  202. /* Print out the IAOQ for debugging */
  203. DBG(1,"setup_sigcontext32: ia0q %#lx / %#lx\n",
  204. regs->iaoq[0], regs->iaoq[1]);
  205. }
  206. err |= __put_user(flags, &sc->sc_flags);
  207. DBG(1,"setup_sigcontext32: Truncating general registers.\n");
  208. for(regn=0; regn < 32; regn++){
  209. /* Truncate a general register */
  210. compat_reg = (compat_uint_t)(regs->gr[regn]);
  211. err |= __put_user(compat_reg, &sc->sc_gr[regn]);
  212. /* Store upper half */
  213. compat_regb = (compat_uint_t)(regs->gr[regn] >> 32);
  214. err |= __put_user(compat_regb, &rf->rf_gr[regn]);
  215. /* DEBUG: Write out the "upper / lower" register data */
  216. DBG(2,"setup_sigcontext32: gr%02d = %#x / %#x\n", regn,
  217. compat_regb, compat_reg);
  218. }
  219. /* Copy the floating point registers (same size)
  220. XXX: BE WARNED FR's are 64-BIT! */
  221. DBG(1,"setup_sigcontext32: Copying from regs to sc, "
  222. "sc->sc_fr size = %#lx, regs->fr size = %#lx\n",
  223. sizeof(regs->fr), sizeof(sc->sc_fr));
  224. err |= __copy_to_user(sc->sc_fr, regs->fr, sizeof(regs->fr));
  225. compat_reg = (compat_uint_t)(regs->sar);
  226. err |= __put_user(compat_reg, &sc->sc_sar);
  227. DBG(2,"setup_sigcontext32: sar is %#x\n", compat_reg);
  228. /* Store upper half */
  229. compat_reg = (compat_uint_t)(regs->sar >> 32);
  230. err |= __put_user(compat_reg, &rf->rf_sar);
  231. DBG(2,"setup_sigcontext32: upper half sar = %#x\n", compat_reg);
  232. DBG(1,"setup_sigcontext32: r28 is %ld\n", regs->gr[28]);
  233. return err;
  234. }
  235. int
  236. copy_siginfo_from_user32 (siginfo_t *to, compat_siginfo_t __user *from)
  237. {
  238. compat_uptr_t addr;
  239. int err;
  240. if (!access_ok(VERIFY_READ, from, sizeof(compat_siginfo_t)))
  241. return -EFAULT;
  242. err = __get_user(to->si_signo, &from->si_signo);
  243. err |= __get_user(to->si_errno, &from->si_errno);
  244. err |= __get_user(to->si_code, &from->si_code);
  245. if (to->si_code < 0)
  246. err |= __copy_from_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
  247. else {
  248. switch (to->si_code >> 16) {
  249. case __SI_CHLD >> 16:
  250. err |= __get_user(to->si_utime, &from->si_utime);
  251. err |= __get_user(to->si_stime, &from->si_stime);
  252. err |= __get_user(to->si_status, &from->si_status);
  253. default:
  254. err |= __get_user(to->si_pid, &from->si_pid);
  255. err |= __get_user(to->si_uid, &from->si_uid);
  256. break;
  257. case __SI_FAULT >> 16:
  258. err |= __get_user(addr, &from->si_addr);
  259. to->si_addr = compat_ptr(addr);
  260. break;
  261. case __SI_POLL >> 16:
  262. err |= __get_user(to->si_band, &from->si_band);
  263. err |= __get_user(to->si_fd, &from->si_fd);
  264. break;
  265. case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
  266. case __SI_MESGQ >> 16:
  267. err |= __get_user(to->si_pid, &from->si_pid);
  268. err |= __get_user(to->si_uid, &from->si_uid);
  269. err |= __get_user(to->si_int, &from->si_int);
  270. break;
  271. }
  272. }
  273. return err;
  274. }
  275. int
  276. copy_siginfo_to_user32 (compat_siginfo_t __user *to, const siginfo_t *from)
  277. {
  278. compat_uptr_t addr;
  279. compat_int_t val;
  280. int err;
  281. if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
  282. return -EFAULT;
  283. /* If you change siginfo_t structure, please be sure
  284. this code is fixed accordingly.
  285. It should never copy any pad contained in the structure
  286. to avoid security leaks, but must copy the generic
  287. 3 ints plus the relevant union member.
  288. This routine must convert siginfo from 64bit to 32bit as well
  289. at the same time. */
  290. err = __put_user(from->si_signo, &to->si_signo);
  291. err |= __put_user(from->si_errno, &to->si_errno);
  292. err |= __put_user((short)from->si_code, &to->si_code);
  293. if (from->si_code < 0)
  294. err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
  295. else {
  296. switch (from->si_code >> 16) {
  297. case __SI_CHLD >> 16:
  298. err |= __put_user(from->si_utime, &to->si_utime);
  299. err |= __put_user(from->si_stime, &to->si_stime);
  300. err |= __put_user(from->si_status, &to->si_status);
  301. default:
  302. err |= __put_user(from->si_pid, &to->si_pid);
  303. err |= __put_user(from->si_uid, &to->si_uid);
  304. break;
  305. case __SI_FAULT >> 16:
  306. addr = ptr_to_compat(from->si_addr);
  307. err |= __put_user(addr, &to->si_addr);
  308. break;
  309. case __SI_POLL >> 16:
  310. err |= __put_user(from->si_band, &to->si_band);
  311. err |= __put_user(from->si_fd, &to->si_fd);
  312. break;
  313. case __SI_TIMER >> 16:
  314. err |= __put_user(from->si_tid, &to->si_tid);
  315. err |= __put_user(from->si_overrun, &to->si_overrun);
  316. val = (compat_int_t)from->si_int;
  317. err |= __put_user(val, &to->si_int);
  318. break;
  319. case __SI_RT >> 16: /* Not generated by the kernel as of now. */
  320. case __SI_MESGQ >> 16:
  321. err |= __put_user(from->si_uid, &to->si_uid);
  322. err |= __put_user(from->si_pid, &to->si_pid);
  323. val = (compat_int_t)from->si_int;
  324. err |= __put_user(val, &to->si_int);
  325. break;
  326. }
  327. }
  328. return err;
  329. }