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