]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/linuxload.c
migration: Introduce pss_channel
[mirror_qemu.git] / linux-user / linuxload.c
CommitLineData
0458df24 1/* Code for loading Linux executables. Mostly linux kernel code. */
e5fe0c52 2
d39594e9 3#include "qemu/osdep.h"
e5fe0c52 4#include "qemu.h"
3b249d26 5#include "user-internals.h"
3ad0a769 6#include "loader.h"
e5fe0c52
PB
7
8#define NGROUPS 32
9
10/* ??? This should really be somewhere else. */
a46955ff 11abi_long memcpy_to_target(abi_ulong dest, const void *src, unsigned long len)
e5fe0c52
PB
12{
13 void *host_ptr;
14
579a97f7 15 host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
a46955ff 16 if (!host_ptr) {
579a97f7 17 return -TARGET_EFAULT;
a46955ff 18 }
e5fe0c52
PB
19 memcpy(host_ptr, src, len);
20 unlock_user(host_ptr, dest, 1);
579a97f7 21 return 0;
e5fe0c52
PB
22}
23
a46955ff 24static int count(char **vec)
e5fe0c52 25{
a46955ff 26 int i;
e5fe0c52 27
a46955ff 28 for (i = 0; *vec; i++) {
e5fe0c52
PB
29 vec++;
30 }
a46955ff 31 return i;
e5fe0c52
PB
32}
33
34static int prepare_binprm(struct linux_binprm *bprm)
35{
a46955ff 36 struct stat st;
e5fe0c52 37 int mode;
331c23b5 38 int retval;
e5fe0c52 39
a46955ff
RH
40 if (fstat(bprm->fd, &st) < 0) {
41 return -errno;
e5fe0c52
PB
42 }
43
44 mode = st.st_mode;
a46955ff
RH
45 if (!S_ISREG(mode)) { /* Must be regular file */
46 return -EACCES;
e5fe0c52 47 }
a46955ff
RH
48 if (!(mode & 0111)) { /* Must have at least one execute bit set */
49 return -EACCES;
e5fe0c52
PB
50 }
51
52 bprm->e_uid = geteuid();
53 bprm->e_gid = getegid();
e5fe0c52
PB
54
55 /* Set-uid? */
a46955ff 56 if (mode & S_ISUID) {
7d37435b 57 bprm->e_uid = st.st_uid;
e5fe0c52
PB
58 }
59
60 /* Set-gid? */
61 /*
62 * If setgid is set but no group execute bit then this
63 * is a candidate for mandatory locking, not a setgid
64 * executable.
65 */
66 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
7d37435b 67 bprm->e_gid = st.st_gid;
e5fe0c52
PB
68 }
69
9955ffac
RH
70 retval = read(bprm->fd, bprm->buf, BPRM_BUF_SIZE);
71 if (retval < 0) {
7d37435b
PB
72 perror("prepare_binprm");
73 exit(-1);
e5fe0c52 74 }
9955ffac
RH
75 if (retval < BPRM_BUF_SIZE) {
76 /* Make sure the rest of the loader won't read garbage. */
77 memset(bprm->buf + retval, 0, BPRM_BUF_SIZE - retval);
e5fe0c52 78 }
9955ffac 79 return retval;
e5fe0c52
PB
80}
81
82/* Construct the envp and argv tables on the target stack. */
992f48a0
BS
83abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
84 abi_ulong stringp, int push_ptr)
e5fe0c52 85{
0429a971 86 TaskState *ts = (TaskState *)thread_cpu->opaque;
992f48a0
BS
87 int n = sizeof(abi_ulong);
88 abi_ulong envp;
89 abi_ulong argv;
e5fe0c52
PB
90
91 sp -= (envc + 1) * n;
92 envp = sp;
93 sp -= (argc + 1) * n;
94 argv = sp;
60f1c801
RH
95 ts->info->envp = envp;
96 ts->info->envc = envc;
97 ts->info->argv = argv;
98 ts->info->argc = argc;
99
e5fe0c52 100 if (push_ptr) {
2f619698
FB
101 /* FIXME - handle put_user() failures */
102 sp -= n;
103 put_user_ual(envp, sp);
104 sp -= n;
105 put_user_ual(argv, sp);
e5fe0c52 106 }
60f1c801 107
2f619698
FB
108 sp -= n;
109 /* FIXME - handle put_user() failures */
110 put_user_ual(argc, sp);
60f1c801
RH
111
112 ts->info->arg_strings = stringp;
e5fe0c52 113 while (argc-- > 0) {
2f619698
FB
114 /* FIXME - handle put_user() failures */
115 put_user_ual(stringp, argv);
116 argv += n;
e5fe0c52
PB
117 stringp += target_strlen(stringp) + 1;
118 }
2f619698
FB
119 /* FIXME - handle put_user() failures */
120 put_user_ual(0, argv);
60f1c801
RH
121
122 ts->info->env_strings = stringp;
e5fe0c52 123 while (envc-- > 0) {
2f619698
FB
124 /* FIXME - handle put_user() failures */
125 put_user_ual(stringp, envp);
126 envp += n;
e5fe0c52
PB
127 stringp += target_strlen(stringp) + 1;
128 }
2f619698
FB
129 /* FIXME - handle put_user() failures */
130 put_user_ual(0, envp);
e5fe0c52
PB
131
132 return sp;
133}
134
03cfd8fa 135int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
a46955ff
RH
136 struct target_pt_regs *regs, struct image_info *infop,
137 struct linux_binprm *bprm)
e5fe0c52 138{
e5fe0c52 139 int retval;
e5fe0c52 140
03cfd8fa 141 bprm->fd = fdexec;
edf8e2af
MW
142 bprm->filename = (char *)filename;
143 bprm->argc = count(argv);
144 bprm->argv = argv;
145 bprm->envc = count(envp);
146 bprm->envp = envp;
e5fe0c52 147
edf8e2af 148 retval = prepare_binprm(bprm);
e5fe0c52 149
a46955ff 150 if (retval >= 0) {
edf8e2af
MW
151 if (bprm->buf[0] == 0x7f
152 && bprm->buf[1] == 'E'
153 && bprm->buf[2] == 'L'
154 && bprm->buf[3] == 'F') {
f0116c54 155 retval = load_elf_binary(bprm, infop);
e5fe0c52 156#if defined(TARGET_HAS_BFLT)
edf8e2af
MW
157 } else if (bprm->buf[0] == 'b'
158 && bprm->buf[1] == 'F'
159 && bprm->buf[2] == 'L'
160 && bprm->buf[3] == 'T') {
f0116c54 161 retval = load_flt_binary(bprm, infop);
e5fe0c52
PB
162#endif
163 } else {
885c1d10 164 return -ENOEXEC;
e5fe0c52
PB
165 }
166 }
3b46e624 167
a46955ff 168 if (retval >= 0) {
e5fe0c52
PB
169 /* success. Initialize important registers */
170 do_init_thread(regs, infop);
171 return retval;
172 }
173
a46955ff 174 return retval;
e5fe0c52 175}