+static void get_target_siginfo(target_siginfo_t *tinfo,
+ const target_siginfo_t *info)
+{
+ abi_ulong sival_ptr;
+
+ int sig;
+ int si_errno;
+ int si_code;
+ int si_type;
+
+ __get_user(sig, &info->si_signo);
+ __get_user(si_errno, &tinfo->si_errno);
+ __get_user(si_code, &info->si_code);
+
+ tinfo->si_signo = sig;
+ tinfo->si_errno = si_errno;
+ tinfo->si_code = si_code;
+
+ /* Ensure we don't leak random junk to the guest later */
+ memset(tinfo->_sifields._pad, 0, sizeof(tinfo->_sifields._pad));
+
+ /* This is awkward, because we have to use a combination of
+ * the si_code and si_signo to figure out which of the union's
+ * members are valid. (Within the host kernel it is always possible
+ * to tell, but the kernel carefully avoids giving userspace the
+ * high 16 bits of si_code, so we don't have the information to
+ * do this the easy way...) We therefore make our best guess,
+ * bearing in mind that a guest can spoof most of the si_codes
+ * via rt_sigqueueinfo() if it likes.
+ *
+ * Once we have made our guess, we record it in the top 16 bits of
+ * the si_code, so that print_siginfo() later can use it.
+ * print_siginfo() will strip these top bits out before printing
+ * the si_code.
+ */
+
+ switch (si_code) {
+ case SI_USER:
+ case SI_TKILL:
+ case SI_KERNEL:
+ /* Sent via kill(), tkill() or tgkill(), or direct from the kernel.
+ * These are the only unspoofable si_code values.
+ */
+ __get_user(tinfo->_sifields._kill._pid, &info->_sifields._kill._pid);
+ __get_user(tinfo->_sifields._kill._uid, &info->_sifields._kill._uid);
+ si_type = QEMU_SI_KILL;
+ break;
+ default:
+ /* Everything else is spoofable. Make best guess based on signal */
+ switch (sig) {
+ case TARGET_SIGCHLD:
+ __get_user(tinfo->_sifields._sigchld._pid,
+ &info->_sifields._sigchld._pid);
+ __get_user(tinfo->_sifields._sigchld._uid,
+ &info->_sifields._sigchld._uid);
+ __get_user(tinfo->_sifields._sigchld._status,
+ &info->_sifields._sigchld._status);
+ __get_user(tinfo->_sifields._sigchld._utime,
+ &info->_sifields._sigchld._utime);
+ __get_user(tinfo->_sifields._sigchld._stime,
+ &info->_sifields._sigchld._stime);
+ si_type = QEMU_SI_CHLD;
+ break;
+ case TARGET_SIGIO:
+ __get_user(tinfo->_sifields._sigpoll._band,
+ &info->_sifields._sigpoll._band);
+ __get_user(tinfo->_sifields._sigpoll._fd,
+ &info->_sifields._sigpoll._fd);
+ si_type = QEMU_SI_POLL;
+ break;
+ default:
+ /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */
+ __get_user(tinfo->_sifields._rt._pid, &info->_sifields._rt._pid);
+ __get_user(tinfo->_sifields._rt._uid, &info->_sifields._rt._uid);
+ /* XXX: potential problem if 64 bit */
+ __get_user(sival_ptr, &info->_sifields._rt._sigval.sival_ptr);
+ tinfo->_sifields._rt._sigval.sival_ptr = sival_ptr;
+
+ si_type = QEMU_SI_RT;
+ break;
+ }
+ break;
+ }
+
+ tinfo->si_code = deposit32(si_code, 16, 16, si_type);
+}
+