]> git.proxmox.com Git - mirror_qemu.git/blame - bsd-user/bsdload.c
bsd-user: Stop building the sparc targets
[mirror_qemu.git] / bsd-user / bsdload.c
CommitLineData
84778508
BS
1/* Code for loading BSD executables. Mostly linux kernel code. */
2
2231197c 3#include "qemu/osdep.h"
84778508
BS
4
5#include "qemu.h"
6
e99a22cc 7#define TARGET_NGROUPS 32
84778508
BS
8
9/* ??? This should really be somewhere else. */
10abi_long memcpy_to_target(abi_ulong dest, const void *src,
11 unsigned long len)
12{
13 void *host_ptr;
14
15 host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
58b3beb4 16 if (!host_ptr) {
84778508 17 return -TARGET_EFAULT;
58b3beb4 18 }
84778508
BS
19 memcpy(host_ptr, src, len);
20 unlock_user(host_ptr, dest, 1);
21 return 0;
22}
23
ca0fd2e3 24static int count(char **vec)
84778508
BS
25{
26 int i;
27
ca0fd2e3 28 for (i = 0; *vec; i++) {
84778508
BS
29 vec++;
30 }
31
fa054637 32 return i;
84778508
BS
33}
34
35static int prepare_binprm(struct linux_binprm *bprm)
36{
37 struct stat st;
38 int mode;
4a65a86a 39 int retval;
84778508 40
ca0fd2e3 41 if (fstat(bprm->fd, &st) < 0) {
fa054637 42 return -errno;
84778508
BS
43 }
44
45 mode = st.st_mode;
ca0fd2e3 46 if (!S_ISREG(mode)) { /* Must be regular file */
fa054637 47 return -EACCES;
84778508 48 }
ca0fd2e3 49 if (!(mode & 0111)) { /* Must have at least one execute bit set */
fa054637 50 return -EACCES;
84778508
BS
51 }
52
53 bprm->e_uid = geteuid();
54 bprm->e_gid = getegid();
84778508
BS
55
56 /* Set-uid? */
ca0fd2e3 57 if (mode & S_ISUID) {
84778508 58 bprm->e_uid = st.st_uid;
84778508
BS
59 }
60
61 /* Set-gid? */
62 /*
63 * If setgid is set but no group execute bit then this
64 * is a candidate for mandatory locking, not a setgid
65 * executable.
66 */
67 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
68 bprm->e_gid = st.st_gid;
84778508
BS
69 }
70
71 memset(bprm->buf, 0, sizeof(bprm->buf));
72 retval = lseek(bprm->fd, 0L, SEEK_SET);
ca0fd2e3 73 if (retval >= 0) {
84778508
BS
74 retval = read(bprm->fd, bprm->buf, 128);
75 }
ca0fd2e3 76 if (retval < 0) {
84778508
BS
77 perror("prepare_binprm");
78 exit(-1);
58b3beb4 79 } else {
fa054637 80 return retval;
84778508
BS
81 }
82}
83
84/* Construct the envp and argv tables on the target stack. */
85abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
86 abi_ulong stringp, int push_ptr)
87{
88 int n = sizeof(abi_ulong);
89 abi_ulong envp;
90 abi_ulong argv;
91
92 sp -= (envc + 1) * n;
93 envp = sp;
94 sp -= (argc + 1) * n;
95 argv = sp;
96 if (push_ptr) {
97 /* FIXME - handle put_user() failures */
98 sp -= n;
99 put_user_ual(envp, sp);
100 sp -= n;
101 put_user_ual(argv, sp);
102 }
103 sp -= n;
104 /* FIXME - handle put_user() failures */
105 put_user_ual(argc, sp);
106
107 while (argc-- > 0) {
108 /* FIXME - handle put_user() failures */
109 put_user_ual(stringp, argv);
110 argv += n;
111 stringp += target_strlen(stringp) + 1;
112 }
113 /* FIXME - handle put_user() failures */
114 put_user_ual(0, argv);
115 while (envc-- > 0) {
116 /* FIXME - handle put_user() failures */
117 put_user_ual(stringp, envp);
118 envp += n;
119 stringp += target_strlen(stringp) + 1;
120 }
121 /* FIXME - handle put_user() failures */
122 put_user_ual(0, envp);
123
124 return sp;
125}
126
ca0fd2e3
WL
127int loader_exec(const char *filename, char **argv, char **envp,
128 struct target_pt_regs *regs, struct image_info *infop)
84778508
BS
129{
130 struct linux_binprm bprm;
131 int retval;
132 int i;
133
ca0fd2e3 134 bprm.p = TARGET_PAGE_SIZE * MAX_ARG_PAGES - sizeof(unsigned int);
58b3beb4 135 for (i = 0 ; i < MAX_ARG_PAGES ; i++) { /* clear page-table */
7cba04f6 136 bprm.page[i] = NULL;
58b3beb4 137 }
84778508 138 retval = open(filename, O_RDONLY);
58b3beb4 139 if (retval < 0) {
84778508 140 return retval;
58b3beb4 141 }
84778508
BS
142 bprm.fd = retval;
143 bprm.filename = (char *)filename;
144 bprm.argc = count(argv);
145 bprm.argv = argv;
146 bprm.envc = count(envp);
147 bprm.envp = envp;
148
149 retval = prepare_binprm(&bprm);
150
ca0fd2e3 151 if (retval >= 0) {
84778508
BS
152 if (bprm.buf[0] == 0x7f
153 && bprm.buf[1] == 'E'
154 && bprm.buf[2] == 'L'
155 && bprm.buf[3] == 'F') {
ca0fd2e3 156 retval = load_elf_binary(&bprm, regs, infop);
84778508 157 } else {
9bb93180 158 fprintf(stderr, "Unknown binary format\n");
84778508
BS
159 return -1;
160 }
161 }
162
ca0fd2e3 163 if (retval >= 0) {
84778508
BS
164 /* success. Initialize important registers */
165 do_init_thread(regs, infop);
166 return retval;
167 }
168
169 /* Something went wrong, return the inode and free the argument pages*/
ca0fd2e3 170 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
c580dee4 171 g_free(bprm.page[i]);
84778508 172 }
fa054637 173 return retval;
84778508 174}