]> git.proxmox.com Git - qemu.git/blame - linux-user/linuxload.c
hw/pci-host/versatile.c: Provide property for forcing broken IRQ mapping
[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{
edf8e2af 92 TaskState *ts = (TaskState *)thread_env->opaque;
992f48a0
BS
93 int n = sizeof(abi_ulong);
94 abi_ulong envp;
95 abi_ulong argv;
e5fe0c52
PB
96
97 sp -= (envc + 1) * n;
98 envp = sp;
99 sp -= (argc + 1) * n;
100 argv = sp;
101 if (push_ptr) {
2f619698
FB
102 /* FIXME - handle put_user() failures */
103 sp -= n;
104 put_user_ual(envp, sp);
105 sp -= n;
106 put_user_ual(argv, sp);
e5fe0c52 107 }
2f619698
FB
108 sp -= n;
109 /* FIXME - handle put_user() failures */
110 put_user_ual(argc, sp);
edf8e2af 111 ts->info->arg_start = stringp;
e5fe0c52 112 while (argc-- > 0) {
2f619698
FB
113 /* FIXME - handle put_user() failures */
114 put_user_ual(stringp, argv);
115 argv += n;
e5fe0c52
PB
116 stringp += target_strlen(stringp) + 1;
117 }
edf8e2af 118 ts->info->arg_end = stringp;
2f619698
FB
119 /* FIXME - handle put_user() failures */
120 put_user_ual(0, argv);
e5fe0c52 121 while (envc-- > 0) {
2f619698
FB
122 /* FIXME - handle put_user() failures */
123 put_user_ual(stringp, envp);
124 envp += n;
e5fe0c52
PB
125 stringp += target_strlen(stringp) + 1;
126 }
2f619698
FB
127 /* FIXME - handle put_user() failures */
128 put_user_ual(0, envp);
e5fe0c52
PB
129
130 return sp;
131}
132
5fafdf24 133int loader_exec(const char * filename, char ** argv, char ** envp,
edf8e2af
MW
134 struct target_pt_regs * regs, struct image_info *infop,
135 struct linux_binprm *bprm)
e5fe0c52 136{
e5fe0c52
PB
137 int retval;
138 int i;
139
edf8e2af 140 bprm->p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
9955ffac 141 memset(bprm->page, 0, sizeof(bprm->page));
e5fe0c52 142 retval = open(filename, O_RDONLY);
885c1d10
PM
143 if (retval < 0) {
144 return -errno;
145 }
edf8e2af
MW
146 bprm->fd = retval;
147 bprm->filename = (char *)filename;
148 bprm->argc = count(argv);
149 bprm->argv = argv;
150 bprm->envc = count(envp);
151 bprm->envp = envp;
e5fe0c52 152
edf8e2af 153 retval = prepare_binprm(bprm);
e5fe0c52
PB
154
155 if(retval>=0) {
edf8e2af
MW
156 if (bprm->buf[0] == 0x7f
157 && bprm->buf[1] == 'E'
158 && bprm->buf[2] == 'L'
159 && bprm->buf[3] == 'F') {
6495a044 160 retval = load_elf_binary(bprm, regs, infop);
e5fe0c52 161#if defined(TARGET_HAS_BFLT)
edf8e2af
MW
162 } else if (bprm->buf[0] == 'b'
163 && bprm->buf[1] == 'F'
164 && bprm->buf[2] == 'L'
165 && bprm->buf[3] == 'T') {
166 retval = load_flt_binary(bprm,regs,infop);
e5fe0c52
PB
167#endif
168 } else {
885c1d10 169 return -ENOEXEC;
e5fe0c52
PB
170 }
171 }
3b46e624 172
e5fe0c52
PB
173 if(retval>=0) {
174 /* success. Initialize important registers */
175 do_init_thread(regs, infop);
176 return retval;
177 }
178
179 /* Something went wrong, return the inode and free the argument pages*/
180 for (i=0 ; i<MAX_ARG_PAGES ; i++) {
7dd47667 181 g_free(bprm->page[i]);
e5fe0c52
PB
182 }
183 return(retval);
184}