Blame view

fs/binfmt_som.c 7.14 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  /*
   * linux/fs/binfmt_som.c
   *
   * These are the functions used to load SOM format executables as used
   * by HP-UX.  
   *
   * Copyright 1999 Matthew Wilcox <willy@bofh.ai>
   * based on binfmt_elf which is
   * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
   */
  
  #include <linux/module.h>
  
  #include <linux/fs.h>
  #include <linux/stat.h>
  #include <linux/sched.h>
  #include <linux/mm.h>
  #include <linux/mman.h>
  #include <linux/errno.h>
  #include <linux/signal.h>
  #include <linux/binfmts.h>
  #include <linux/som.h>
  #include <linux/string.h>
  #include <linux/file.h>
  #include <linux/fcntl.h>
  #include <linux/ptrace.h>
  #include <linux/slab.h>
  #include <linux/shm.h>
  #include <linux/personality.h>
  #include <linux/init.h>
  
  #include <asm/uaccess.h>
  #include <asm/pgtable.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
  
  #include <linux/elf.h>
71613c3b8   Al Viro   get rid of pt_reg...
36
  static int load_som_binary(struct linux_binprm * bprm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
39
40
41
42
43
  static int load_som_library(struct file *);
  
  /*
   * If we don't support core dumping, then supply a NULL so we
   * don't even try.
   */
  #if 0
f6151dfea   Masami Hiramatsu   mm: introduce cor...
44
  static int som_core_dump(struct coredump_params *cprm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  #else
  #define som_core_dump	NULL
  #endif
  
  #define SOM_PAGESTART(_v) ((_v) & ~(unsigned long)(SOM_PAGESIZE-1))
  #define SOM_PAGEOFFSET(_v) ((_v) & (SOM_PAGESIZE-1))
  #define SOM_PAGEALIGN(_v) (((_v) + SOM_PAGESIZE - 1) & ~(SOM_PAGESIZE - 1))
  
  static struct linux_binfmt som_format = {
  	.module		= THIS_MODULE,
  	.load_binary	= load_som_binary,
  	.load_shlib	= load_som_library,
  	.core_dump	= som_core_dump,
  	.min_coredump	= SOM_PAGESIZE
  };
  
  /*
   * create_som_tables() parses the env- and arg-strings in new user
   * memory and creates the pointer tables from them, and puts their
   * addresses on the "stack", returning the new stack pointer value.
   */
  static void create_som_tables(struct linux_binprm *bprm)
  {
  	char **argv, **envp;
  	int argc = bprm->argc;
  	int envc = bprm->envc;
  	unsigned long p;
  	unsigned long *sp;
  
  	/* Word-align the stack pointer */
  	sp = (unsigned long *)((bprm->p + 3) & ~3);
  
  	envp = (char **) sp;
  	sp += envc + 1;
  	argv = (char **) sp;
  	sp += argc + 1;
  
  	__put_user((unsigned long) envp,++sp);
  	__put_user((unsigned long) argv,++sp);
  
  	__put_user(argc, ++sp);
  
  	bprm->p = (unsigned long) sp;
  
  	p = current->mm->arg_start;
  	while (argc-- > 0) {
  		__put_user((char *)p,argv++);
  		p += strlen_user((char *)p);
  	}
  	__put_user(NULL, argv);
  	current->mm->arg_end = current->mm->env_start = p;
  	while (envc-- > 0) {
  		__put_user((char *)p,envp++);
  		p += strlen_user((char *)p);
  	}
  	__put_user(NULL, envp);
  	current->mm->env_end = p;
  }
  
  static int check_som_header(struct som_hdr *som_ex)
  {
  	int *buf = (int *)som_ex;
  	int i, ck;
  
  	if (som_ex->system_id != SOM_SID_PARISC_1_0 &&
  	    som_ex->system_id != SOM_SID_PARISC_1_1 &&
  	    som_ex->system_id != SOM_SID_PARISC_2_0)
  		return -ENOEXEC;
  
  	if (som_ex->a_magic != SOM_EXEC_NONSHARE &&
  	    som_ex->a_magic != SOM_EXEC_SHARE &&
  	    som_ex->a_magic != SOM_EXEC_DEMAND)
  		return -ENOEXEC;
  
  	if (som_ex->version_id != SOM_ID_OLD &&
  	    som_ex->version_id != SOM_ID_NEW)
  		return -ENOEXEC;
  
  	ck = 0;
  	for (i=0; i<32; i++)
  		ck ^= buf[i];
  	if (ck != 0)
  		return -ENOEXEC;
  
  	return 0;
  }
  
  static int map_som_binary(struct file *file,
  		const struct som_exec_auxhdr *hpuxhdr)
  {
  	unsigned long code_start, code_size, data_start, data_size;
  	unsigned long bss_start, som_brk;
  	int retval;
  	int prot = PROT_READ | PROT_EXEC;
  	int flags = MAP_FIXED|MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;
  
  	mm_segment_t old_fs = get_fs();
  	set_fs(get_ds());
  
  	code_start = SOM_PAGESTART(hpuxhdr->exec_tmem);
  	code_size = SOM_PAGEALIGN(hpuxhdr->exec_tsize);
  	current->mm->start_code = code_start;
  	current->mm->end_code = code_start + code_size;
6be5ceb02   Linus Torvalds   VM: add "vm_mmap(...
148
  	retval = vm_mmap(file, code_start, code_size, prot,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
  			flags, SOM_PAGESTART(hpuxhdr->exec_tfile));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
151
152
153
154
155
156
  	if (retval < 0 && retval > -1024)
  		goto out;
  
  	data_start = SOM_PAGESTART(hpuxhdr->exec_dmem);
  	data_size = SOM_PAGEALIGN(hpuxhdr->exec_dsize);
  	current->mm->start_data = data_start;
  	current->mm->end_data = bss_start = data_start + data_size;
6be5ceb02   Linus Torvalds   VM: add "vm_mmap(...
157
  	retval = vm_mmap(file, data_start, data_size,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
  			prot | PROT_WRITE, flags,
  			SOM_PAGESTART(hpuxhdr->exec_dfile));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
161
162
163
164
  	if (retval < 0 && retval > -1024)
  		goto out;
  
  	som_brk = bss_start + SOM_PAGEALIGN(hpuxhdr->exec_bsize);
  	current->mm->start_brk = current->mm->brk = som_brk;
6be5ceb02   Linus Torvalds   VM: add "vm_mmap(...
165
  	retval = vm_mmap(NULL, bss_start, som_brk - bss_start,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
  			prot | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  	if (retval > 0 || retval < -1024)
  		retval = 0;
  out:
  	set_fs(old_fs);
  	return retval;
  }
  
  
  /*
   * These are the functions used to load SOM executables and shared
   * libraries.  There is no binary dependent code anywhere else.
   */
  
  static int
71613c3b8   Al Viro   get rid of pt_reg...
181
  load_som_binary(struct linux_binprm * bprm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
185
186
187
  	int retval;
  	unsigned int size;
  	unsigned long som_entry;
  	struct som_hdr *som_ex;
  	struct som_exec_auxhdr *hpuxhdr;
71613c3b8   Al Viro   get rid of pt_reg...
188
  	struct pt_regs *regs = current_pt_regs();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  
  	/* Get the exec-header */
  	som_ex = (struct som_hdr *) bprm->buf;
  
  	retval = check_som_header(som_ex);
  	if (retval != 0)
  		goto out;
  
  	/* Now read in the auxiliary header information */
  
  	retval = -ENOMEM;
  	size = som_ex->aux_header_size;
  	if (size > SOM_PAGESIZE)
  		goto out;
dc02747da   Matthew Wilcox   [PARISC] Fix fs/b...
203
  	hpuxhdr = kmalloc(size, GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204
205
206
207
208
  	if (!hpuxhdr)
  		goto out;
  
  	retval = kernel_read(bprm->file, som_ex->aux_header_location,
  			(char *) hpuxhdr, size);
dc02747da   Matthew Wilcox   [PARISC] Fix fs/b...
209
210
211
212
213
  	if (retval != size) {
  		if (retval >= 0)
  			retval = -EIO;
  		goto out_free;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
215
216
217
218
219
  	/* Flush all traces of the currently running executable */
  	retval = flush_old_exec(bprm);
  	if (retval)
  		goto out_free;
  
  	/* OK, This is the point of no return */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
  	current->personality = PER_HPUX;
221af7f87   Linus Torvalds   Split 'flush_old_...
221
  	setup_new_exec(bprm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  
  	/* Set the task size for HP-UX processes such that
  	 * the gateway page is outside the address space.
  	 * This can be fixed later, but for now, this is much
  	 * easier.
  	 */
  
  	current->thread.task_size = 0xc0000000;
  
  	/* Set map base to allow enough room for hp-ux heap growth */
  
  	current->thread.map_base = 0x80000000;
  
  	retval = map_som_binary(bprm->file, hpuxhdr);
  	if (retval < 0)
  		goto out_free;
  
  	som_entry = hpuxhdr->exec_entry;
  	kfree(hpuxhdr);
  
  	set_binfmt(&som_format);
a6f76f23d   David Howells   CRED: Make execve...
243
  	install_exec_creds(bprm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
246
247
248
  	setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT);
  
  	create_som_tables(bprm);
  
  	current->mm->start_stack = bprm->p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  
  #if 0
  	printk("(start_brk) %08lx
  " , (unsigned long) current->mm->start_brk);
  	printk("(end_code) %08lx
  " , (unsigned long) current->mm->end_code);
  	printk("(start_code) %08lx
  " , (unsigned long) current->mm->start_code);
  	printk("(end_data) %08lx
  " , (unsigned long) current->mm->end_data);
  	printk("(start_stack) %08lx
  " , (unsigned long) current->mm->start_stack);
  	printk("(brk) %08lx
  " , (unsigned long) current->mm->brk);
  #endif
  
  	map_hpux_gateway_page(current,current->mm);
  
  	start_thread_som(regs, som_entry, bprm->p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  	return 0;
  
  	/* error cleanup */
  out_free:
  	kfree(hpuxhdr);
  out:
  	return retval;
  }
  
  static int load_som_library(struct file *f)
  {
  /* No lib support in SOM yet.  gizza chance.. */
  	return -ENOEXEC;
  }
  	/* Install the SOM loader.
  	 * N.B. We *rely* on the table being the right size with the
  	 * right number of free slots...
  	 */
  
  static int __init init_som_binfmt(void)
  {
8fc3dc5a3   Al Viro   __register_binfmt...
289
290
  	register_binfmt(&som_format);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
292
293
294
295
296
297
298
299
300
  }
  
  static void __exit exit_som_binfmt(void)
  {
  	/* Remove the SOM loader. */
  	unregister_binfmt(&som_format);
  }
  
  core_initcall(init_som_binfmt);
  module_exit(exit_som_binfmt);
cde162c2a   Adrian Bunk   binfmt_som.c: add...
301
302
  
  MODULE_LICENSE("GPL");