]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/linuxload.c
Revert "vl: Fix to create migration object before block backends again"
[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
PB
4
5#include "qemu.h"
6
7#define NGROUPS 32
8
9/* ??? This should really be somewhere else. */
579a97f7
FB
10abi_long memcpy_to_target(abi_ulong dest, const void *src,
11 unsigned long len)
e5fe0c52
PB
12{
13 void *host_ptr;
14
579a97f7
FB
15 host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
16 if (!host_ptr)
17 return -TARGET_EFAULT;
e5fe0c52
PB
18 memcpy(host_ptr, src, len);
19 unlock_user(host_ptr, dest, 1);
579a97f7 20 return 0;
e5fe0c52
PB
21}
22
e5fe0c52
PB
23static int count(char ** vec)
24{
25 int i;
26
27 for(i = 0; *vec; i++) {
28 vec++;
29 }
30
31 return(i);
32}
33
34static int prepare_binprm(struct linux_binprm *bprm)
35{
36 struct stat st;
37 int mode;
331c23b5 38 int retval;
e5fe0c52
PB
39
40 if(fstat(bprm->fd, &st) < 0) {
7d37435b 41 return(-errno);
e5fe0c52
PB
42 }
43
44 mode = st.st_mode;
45 if(!S_ISREG(mode)) { /* Must be regular file */
7d37435b 46 return(-EACCES);
e5fe0c52
PB
47 }
48 if(!(mode & 0111)) { /* Must have at least one execute bit set */
7d37435b 49 return(-EACCES);
e5fe0c52
PB
50 }
51
52 bprm->e_uid = geteuid();
53 bprm->e_gid = getegid();
e5fe0c52
PB
54
55 /* Set-uid? */
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;
95 if (push_ptr) {
2f619698
FB
96 /* FIXME - handle put_user() failures */
97 sp -= n;
98 put_user_ual(envp, sp);
99 sp -= n;
100 put_user_ual(argv, sp);
e5fe0c52 101 }
2f619698
FB
102 sp -= n;
103 /* FIXME - handle put_user() failures */
104 put_user_ual(argc, sp);
edf8e2af 105 ts->info->arg_start = stringp;
e5fe0c52 106 while (argc-- > 0) {
2f619698
FB
107 /* FIXME - handle put_user() failures */
108 put_user_ual(stringp, argv);
109 argv += n;
e5fe0c52
PB
110 stringp += target_strlen(stringp) + 1;
111 }
edf8e2af 112 ts->info->arg_end = stringp;
2f619698
FB
113 /* FIXME - handle put_user() failures */
114 put_user_ual(0, argv);
e5fe0c52 115 while (envc-- > 0) {
2f619698
FB
116 /* FIXME - handle put_user() failures */
117 put_user_ual(stringp, envp);
118 envp += n;
e5fe0c52
PB
119 stringp += target_strlen(stringp) + 1;
120 }
2f619698
FB
121 /* FIXME - handle put_user() failures */
122 put_user_ual(0, envp);
e5fe0c52
PB
123
124 return sp;
125}
126
03cfd8fa 127int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
edf8e2af
MW
128 struct target_pt_regs * regs, struct image_info *infop,
129 struct linux_binprm *bprm)
e5fe0c52 130{
e5fe0c52 131 int retval;
e5fe0c52 132
03cfd8fa 133 bprm->fd = fdexec;
edf8e2af
MW
134 bprm->filename = (char *)filename;
135 bprm->argc = count(argv);
136 bprm->argv = argv;
137 bprm->envc = count(envp);
138 bprm->envp = envp;
e5fe0c52 139
edf8e2af 140 retval = prepare_binprm(bprm);
e5fe0c52
PB
141
142 if(retval>=0) {
edf8e2af
MW
143 if (bprm->buf[0] == 0x7f
144 && bprm->buf[1] == 'E'
145 && bprm->buf[2] == 'L'
146 && bprm->buf[3] == 'F') {
f0116c54 147 retval = load_elf_binary(bprm, infop);
e5fe0c52 148#if defined(TARGET_HAS_BFLT)
edf8e2af
MW
149 } else if (bprm->buf[0] == 'b'
150 && bprm->buf[1] == 'F'
151 && bprm->buf[2] == 'L'
152 && bprm->buf[3] == 'T') {
f0116c54 153 retval = load_flt_binary(bprm, infop);
e5fe0c52
PB
154#endif
155 } else {
885c1d10 156 return -ENOEXEC;
e5fe0c52
PB
157 }
158 }
3b46e624 159
e5fe0c52
PB
160 if(retval>=0) {
161 /* success. Initialize important registers */
162 do_init_thread(regs, infop);
163 return retval;
164 }
165
e5fe0c52
PB
166 return(retval);
167}