vpe.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2004, 2005 MIPS Technologies, Inc. All rights reserved.
  7. * Copyright (C) 2013 Imagination Technologies Ltd.
  8. *
  9. * VPE spport module for loading a MIPS SP program into VPE1. The SP
  10. * environment is rather simple since there are no TLBs. It needs
  11. * to be relocatable (or partiall linked). Initialize your stack in
  12. * the startup-code. The loader looks for the symbol __start and sets
  13. * up the execution to resume from there. To load and run, simply do
  14. * a cat SP 'binary' to the /dev/vpe1 device.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/device.h>
  18. #include <linux/fs.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/list.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/elf.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/moduleloader.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/poll.h>
  29. #include <linux/bootmem.h>
  30. #include <asm/mipsregs.h>
  31. #include <asm/mipsmtregs.h>
  32. #include <asm/cacheflush.h>
  33. #include <linux/atomic.h>
  34. #include <asm/mips_mt.h>
  35. #include <asm/processor.h>
  36. #include <asm/vpe.h>
  37. #ifndef ARCH_SHF_SMALL
  38. #define ARCH_SHF_SMALL 0
  39. #endif
  40. /* If this is set, the section belongs in the init part of the module */
  41. #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
  42. struct vpe_control vpecontrol = {
  43. .vpe_list_lock = __SPIN_LOCK_UNLOCKED(vpe_list_lock),
  44. .vpe_list = LIST_HEAD_INIT(vpecontrol.vpe_list),
  45. .tc_list_lock = __SPIN_LOCK_UNLOCKED(tc_list_lock),
  46. .tc_list = LIST_HEAD_INIT(vpecontrol.tc_list)
  47. };
  48. /* get the vpe associated with this minor */
  49. struct vpe *get_vpe(int minor)
  50. {
  51. struct vpe *res, *v;
  52. if (!cpu_has_mipsmt)
  53. return NULL;
  54. res = NULL;
  55. spin_lock(&vpecontrol.vpe_list_lock);
  56. list_for_each_entry(v, &vpecontrol.vpe_list, list) {
  57. if (v->minor == VPE_MODULE_MINOR) {
  58. res = v;
  59. break;
  60. }
  61. }
  62. spin_unlock(&vpecontrol.vpe_list_lock);
  63. return res;
  64. }
  65. /* get the vpe associated with this minor */
  66. struct tc *get_tc(int index)
  67. {
  68. struct tc *res, *t;
  69. res = NULL;
  70. spin_lock(&vpecontrol.tc_list_lock);
  71. list_for_each_entry(t, &vpecontrol.tc_list, list) {
  72. if (t->index == index) {
  73. res = t;
  74. break;
  75. }
  76. }
  77. spin_unlock(&vpecontrol.tc_list_lock);
  78. return res;
  79. }
  80. /* allocate a vpe and associate it with this minor (or index) */
  81. struct vpe *alloc_vpe(int minor)
  82. {
  83. struct vpe *v;
  84. v = kzalloc(sizeof(struct vpe), GFP_KERNEL);
  85. if (v == NULL)
  86. goto out;
  87. INIT_LIST_HEAD(&v->tc);
  88. spin_lock(&vpecontrol.vpe_list_lock);
  89. list_add_tail(&v->list, &vpecontrol.vpe_list);
  90. spin_unlock(&vpecontrol.vpe_list_lock);
  91. INIT_LIST_HEAD(&v->notify);
  92. v->minor = VPE_MODULE_MINOR;
  93. out:
  94. return v;
  95. }
  96. /* allocate a tc. At startup only tc0 is running, all other can be halted. */
  97. struct tc *alloc_tc(int index)
  98. {
  99. struct tc *tc;
  100. tc = kzalloc(sizeof(struct tc), GFP_KERNEL);
  101. if (tc == NULL)
  102. goto out;
  103. INIT_LIST_HEAD(&tc->tc);
  104. tc->index = index;
  105. spin_lock(&vpecontrol.tc_list_lock);
  106. list_add_tail(&tc->list, &vpecontrol.tc_list);
  107. spin_unlock(&vpecontrol.tc_list_lock);
  108. out:
  109. return tc;
  110. }
  111. /* clean up and free everything */
  112. void release_vpe(struct vpe *v)
  113. {
  114. list_del(&v->list);
  115. if (v->load_addr)
  116. release_progmem(v);
  117. kfree(v);
  118. }
  119. /* Find some VPE program space */
  120. void *alloc_progmem(unsigned long len)
  121. {
  122. void *addr;
  123. #ifdef CONFIG_MIPS_VPE_LOADER_TOM
  124. /*
  125. * This means you must tell Linux to use less memory than you
  126. * physically have, for example by passing a mem= boot argument.
  127. */
  128. addr = pfn_to_kaddr(max_low_pfn);
  129. memset(addr, 0, len);
  130. #else
  131. /* simple grab some mem for now */
  132. addr = kzalloc(len, GFP_KERNEL);
  133. #endif
  134. return addr;
  135. }
  136. void release_progmem(void *ptr)
  137. {
  138. #ifndef CONFIG_MIPS_VPE_LOADER_TOM
  139. kfree(ptr);
  140. #endif
  141. }
  142. /* Update size with this section: return offset. */
  143. static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
  144. {
  145. long ret;
  146. ret = ALIGN(*size, sechdr->sh_addralign ? : 1);
  147. *size = ret + sechdr->sh_size;
  148. return ret;
  149. }
  150. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  151. might -- code, read-only data, read-write data, small data. Tally
  152. sizes, and place the offsets into sh_entsize fields: high bit means it
  153. belongs in init. */
  154. static void layout_sections(struct module *mod, const Elf_Ehdr *hdr,
  155. Elf_Shdr *sechdrs, const char *secstrings)
  156. {
  157. static unsigned long const masks[][2] = {
  158. /* NOTE: all executable code must be the first section
  159. * in this array; otherwise modify the text_size
  160. * finder in the two loops below */
  161. {SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL},
  162. {SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL},
  163. {SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL},
  164. {ARCH_SHF_SMALL | SHF_ALLOC, 0}
  165. };
  166. unsigned int m, i;
  167. for (i = 0; i < hdr->e_shnum; i++)
  168. sechdrs[i].sh_entsize = ~0UL;
  169. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  170. for (i = 0; i < hdr->e_shnum; ++i) {
  171. Elf_Shdr *s = &sechdrs[i];
  172. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  173. || (s->sh_flags & masks[m][1])
  174. || s->sh_entsize != ~0UL)
  175. continue;
  176. s->sh_entsize =
  177. get_offset((unsigned long *)&mod->core_size, s);
  178. }
  179. if (m == 0)
  180. mod->core_text_size = mod->core_size;
  181. }
  182. }
  183. /* from module-elf32.c, but subverted a little */
  184. struct mips_hi16 {
  185. struct mips_hi16 *next;
  186. Elf32_Addr *addr;
  187. Elf32_Addr value;
  188. };
  189. static struct mips_hi16 *mips_hi16_list;
  190. static unsigned int gp_offs, gp_addr;
  191. static int apply_r_mips_none(struct module *me, uint32_t *location,
  192. Elf32_Addr v)
  193. {
  194. return 0;
  195. }
  196. static int apply_r_mips_gprel16(struct module *me, uint32_t *location,
  197. Elf32_Addr v)
  198. {
  199. int rel;
  200. if (!(*location & 0xffff)) {
  201. rel = (int)v - gp_addr;
  202. } else {
  203. /* .sbss + gp(relative) + offset */
  204. /* kludge! */
  205. rel = (int)(short)((int)v + gp_offs +
  206. (int)(short)(*location & 0xffff) - gp_addr);
  207. }
  208. if ((rel > 32768) || (rel < -32768)) {
  209. pr_debug("VPE loader: apply_r_mips_gprel16: relative address 0x%x out of range of gp register\n",
  210. rel);
  211. return -ENOEXEC;
  212. }
  213. *location = (*location & 0xffff0000) | (rel & 0xffff);
  214. return 0;
  215. }
  216. static int apply_r_mips_pc16(struct module *me, uint32_t *location,
  217. Elf32_Addr v)
  218. {
  219. int rel;
  220. rel = (((unsigned int)v - (unsigned int)location));
  221. rel >>= 2; /* because the offset is in _instructions_ not bytes. */
  222. rel -= 1; /* and one instruction less due to the branch delay slot. */
  223. if ((rel > 32768) || (rel < -32768)) {
  224. pr_debug("VPE loader: apply_r_mips_pc16: relative address out of range 0x%x\n",
  225. rel);
  226. return -ENOEXEC;
  227. }
  228. *location = (*location & 0xffff0000) | (rel & 0xffff);
  229. return 0;
  230. }
  231. static int apply_r_mips_32(struct module *me, uint32_t *location,
  232. Elf32_Addr v)
  233. {
  234. *location += v;
  235. return 0;
  236. }
  237. static int apply_r_mips_26(struct module *me, uint32_t *location,
  238. Elf32_Addr v)
  239. {
  240. if (v % 4) {
  241. pr_debug("VPE loader: apply_r_mips_26: unaligned relocation\n");
  242. return -ENOEXEC;
  243. }
  244. /*
  245. * Not desperately convinced this is a good check of an overflow condition
  246. * anyway. But it gets in the way of handling undefined weak symbols which
  247. * we want to set to zero.
  248. * if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  249. * printk(KERN_ERR
  250. * "module %s: relocation overflow\n",
  251. * me->name);
  252. * return -ENOEXEC;
  253. * }
  254. */
  255. *location = (*location & ~0x03ffffff) |
  256. ((*location + (v >> 2)) & 0x03ffffff);
  257. return 0;
  258. }
  259. static int apply_r_mips_hi16(struct module *me, uint32_t *location,
  260. Elf32_Addr v)
  261. {
  262. struct mips_hi16 *n;
  263. /*
  264. * We cannot relocate this one now because we don't know the value of
  265. * the carry we need to add. Save the information, and let LO16 do the
  266. * actual relocation.
  267. */
  268. n = kmalloc(sizeof(*n), GFP_KERNEL);
  269. if (!n)
  270. return -ENOMEM;
  271. n->addr = location;
  272. n->value = v;
  273. n->next = mips_hi16_list;
  274. mips_hi16_list = n;
  275. return 0;
  276. }
  277. static int apply_r_mips_lo16(struct module *me, uint32_t *location,
  278. Elf32_Addr v)
  279. {
  280. unsigned long insnlo = *location;
  281. Elf32_Addr val, vallo;
  282. struct mips_hi16 *l, *next;
  283. /* Sign extend the addend we extract from the lo insn. */
  284. vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
  285. if (mips_hi16_list != NULL) {
  286. l = mips_hi16_list;
  287. while (l != NULL) {
  288. unsigned long insn;
  289. /*
  290. * The value for the HI16 had best be the same.
  291. */
  292. if (v != l->value) {
  293. pr_debug("VPE loader: apply_r_mips_lo16/hi16: inconsistent value information\n");
  294. goto out_free;
  295. }
  296. /*
  297. * Do the HI16 relocation. Note that we actually don't
  298. * need to know anything about the LO16 itself, except
  299. * where to find the low 16 bits of the addend needed
  300. * by the LO16.
  301. */
  302. insn = *l->addr;
  303. val = ((insn & 0xffff) << 16) + vallo;
  304. val += v;
  305. /*
  306. * Account for the sign extension that will happen in
  307. * the low bits.
  308. */
  309. val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
  310. insn = (insn & ~0xffff) | val;
  311. *l->addr = insn;
  312. next = l->next;
  313. kfree(l);
  314. l = next;
  315. }
  316. mips_hi16_list = NULL;
  317. }
  318. /*
  319. * Ok, we're done with the HI16 relocs. Now deal with the LO16.
  320. */
  321. val = v + vallo;
  322. insnlo = (insnlo & ~0xffff) | (val & 0xffff);
  323. *location = insnlo;
  324. return 0;
  325. out_free:
  326. while (l != NULL) {
  327. next = l->next;
  328. kfree(l);
  329. l = next;
  330. }
  331. mips_hi16_list = NULL;
  332. return -ENOEXEC;
  333. }
  334. static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
  335. Elf32_Addr v) = {
  336. [R_MIPS_NONE] = apply_r_mips_none,
  337. [R_MIPS_32] = apply_r_mips_32,
  338. [R_MIPS_26] = apply_r_mips_26,
  339. [R_MIPS_HI16] = apply_r_mips_hi16,
  340. [R_MIPS_LO16] = apply_r_mips_lo16,
  341. [R_MIPS_GPREL16] = apply_r_mips_gprel16,
  342. [R_MIPS_PC16] = apply_r_mips_pc16
  343. };
  344. static char *rstrs[] = {
  345. [R_MIPS_NONE] = "MIPS_NONE",
  346. [R_MIPS_32] = "MIPS_32",
  347. [R_MIPS_26] = "MIPS_26",
  348. [R_MIPS_HI16] = "MIPS_HI16",
  349. [R_MIPS_LO16] = "MIPS_LO16",
  350. [R_MIPS_GPREL16] = "MIPS_GPREL16",
  351. [R_MIPS_PC16] = "MIPS_PC16"
  352. };
  353. static int apply_relocations(Elf32_Shdr *sechdrs,
  354. const char *strtab,
  355. unsigned int symindex,
  356. unsigned int relsec,
  357. struct module *me)
  358. {
  359. Elf32_Rel *rel = (void *) sechdrs[relsec].sh_addr;
  360. Elf32_Sym *sym;
  361. uint32_t *location;
  362. unsigned int i;
  363. Elf32_Addr v;
  364. int res;
  365. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  366. Elf32_Word r_info = rel[i].r_info;
  367. /* This is where to make the change */
  368. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  369. + rel[i].r_offset;
  370. /* This is the symbol it is referring to */
  371. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  372. + ELF32_R_SYM(r_info);
  373. if (!sym->st_value) {
  374. pr_debug("%s: undefined weak symbol %s\n",
  375. me->name, strtab + sym->st_name);
  376. /* just print the warning, dont barf */
  377. }
  378. v = sym->st_value;
  379. res = reloc_handlers[ELF32_R_TYPE(r_info)](me, location, v);
  380. if (res) {
  381. char *r = rstrs[ELF32_R_TYPE(r_info)];
  382. pr_warn("VPE loader: .text+0x%x relocation type %s for symbol \"%s\" failed\n",
  383. rel[i].r_offset, r ? r : "UNKNOWN",
  384. strtab + sym->st_name);
  385. return res;
  386. }
  387. }
  388. return 0;
  389. }
  390. static inline void save_gp_address(unsigned int secbase, unsigned int rel)
  391. {
  392. gp_addr = secbase + rel;
  393. gp_offs = gp_addr - (secbase & 0xffff0000);
  394. }
  395. /* end module-elf32.c */
  396. /* Change all symbols so that sh_value encodes the pointer directly. */
  397. static void simplify_symbols(Elf_Shdr *sechdrs,
  398. unsigned int symindex,
  399. const char *strtab,
  400. const char *secstrings,
  401. unsigned int nsecs, struct module *mod)
  402. {
  403. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  404. unsigned long secbase, bssbase = 0;
  405. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  406. int size;
  407. /* find the .bss section for COMMON symbols */
  408. for (i = 0; i < nsecs; i++) {
  409. if (strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) == 0) {
  410. bssbase = sechdrs[i].sh_addr;
  411. break;
  412. }
  413. }
  414. for (i = 1; i < n; i++) {
  415. switch (sym[i].st_shndx) {
  416. case SHN_COMMON:
  417. /* Allocate space for the symbol in the .bss section.
  418. st_value is currently size.
  419. We want it to have the address of the symbol. */
  420. size = sym[i].st_value;
  421. sym[i].st_value = bssbase;
  422. bssbase += size;
  423. break;
  424. case SHN_ABS:
  425. /* Don't need to do anything */
  426. break;
  427. case SHN_UNDEF:
  428. /* ret = -ENOENT; */
  429. break;
  430. case SHN_MIPS_SCOMMON:
  431. pr_debug("simplify_symbols: ignoring SHN_MIPS_SCOMMON symbol <%s> st_shndx %d\n",
  432. strtab + sym[i].st_name, sym[i].st_shndx);
  433. /* .sbss section */
  434. break;
  435. default:
  436. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  437. if (strncmp(strtab + sym[i].st_name, "_gp", 3) == 0)
  438. save_gp_address(secbase, sym[i].st_value);
  439. sym[i].st_value += secbase;
  440. break;
  441. }
  442. }
  443. }
  444. #ifdef DEBUG_ELFLOADER
  445. static void dump_elfsymbols(Elf_Shdr *sechdrs, unsigned int symindex,
  446. const char *strtab, struct module *mod)
  447. {
  448. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  449. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  450. pr_debug("dump_elfsymbols: n %d\n", n);
  451. for (i = 1; i < n; i++) {
  452. pr_debug(" i %d name <%s> 0x%x\n", i, strtab + sym[i].st_name,
  453. sym[i].st_value);
  454. }
  455. }
  456. #endif
  457. static int find_vpe_symbols(struct vpe *v, Elf_Shdr *sechdrs,
  458. unsigned int symindex, const char *strtab,
  459. struct module *mod)
  460. {
  461. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  462. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  463. for (i = 1; i < n; i++) {
  464. if (strcmp(strtab + sym[i].st_name, "__start") == 0)
  465. v->__start = sym[i].st_value;
  466. if (strcmp(strtab + sym[i].st_name, "vpe_shared") == 0)
  467. v->shared_ptr = (void *)sym[i].st_value;
  468. }
  469. if ((v->__start == 0) || (v->shared_ptr == NULL))
  470. return -1;
  471. return 0;
  472. }
  473. /*
  474. * Allocates a VPE with some program code space(the load address), copies the
  475. * contents of the program (p)buffer performing relocatations/etc, free's it
  476. * when finished.
  477. */
  478. static int vpe_elfload(struct vpe *v)
  479. {
  480. Elf_Ehdr *hdr;
  481. Elf_Shdr *sechdrs;
  482. long err = 0;
  483. char *secstrings, *strtab = NULL;
  484. unsigned int len, i, symindex = 0, strindex = 0, relocate = 0;
  485. struct module mod; /* so we can re-use the relocations code */
  486. memset(&mod, 0, sizeof(struct module));
  487. strcpy(mod.name, "VPE loader");
  488. hdr = (Elf_Ehdr *) v->pbuffer;
  489. len = v->plen;
  490. /* Sanity checks against insmoding binaries or wrong arch,
  491. weird elf version */
  492. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
  493. || (hdr->e_type != ET_REL && hdr->e_type != ET_EXEC)
  494. || !elf_check_arch(hdr)
  495. || hdr->e_shentsize != sizeof(*sechdrs)) {
  496. pr_warn("VPE loader: program wrong arch or weird elf version\n");
  497. return -ENOEXEC;
  498. }
  499. if (hdr->e_type == ET_REL)
  500. relocate = 1;
  501. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
  502. pr_err("VPE loader: program length %u truncated\n", len);
  503. return -ENOEXEC;
  504. }
  505. /* Convenience variables */
  506. sechdrs = (void *)hdr + hdr->e_shoff;
  507. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  508. sechdrs[0].sh_addr = 0;
  509. /* And these should exist, but gcc whinges if we don't init them */
  510. symindex = strindex = 0;
  511. if (relocate) {
  512. for (i = 1; i < hdr->e_shnum; i++) {
  513. if ((sechdrs[i].sh_type != SHT_NOBITS) &&
  514. (len < sechdrs[i].sh_offset + sechdrs[i].sh_size)) {
  515. pr_err("VPE program length %u truncated\n",
  516. len);
  517. return -ENOEXEC;
  518. }
  519. /* Mark all sections sh_addr with their address in the
  520. temporary image. */
  521. sechdrs[i].sh_addr = (size_t) hdr +
  522. sechdrs[i].sh_offset;
  523. /* Internal symbols and strings. */
  524. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  525. symindex = i;
  526. strindex = sechdrs[i].sh_link;
  527. strtab = (char *)hdr +
  528. sechdrs[strindex].sh_offset;
  529. }
  530. }
  531. layout_sections(&mod, hdr, sechdrs, secstrings);
  532. }
  533. v->load_addr = alloc_progmem(mod.core_size);
  534. if (!v->load_addr)
  535. return -ENOMEM;
  536. pr_info("VPE loader: loading to %p\n", v->load_addr);
  537. if (relocate) {
  538. for (i = 0; i < hdr->e_shnum; i++) {
  539. void *dest;
  540. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  541. continue;
  542. dest = v->load_addr + sechdrs[i].sh_entsize;
  543. if (sechdrs[i].sh_type != SHT_NOBITS)
  544. memcpy(dest, (void *)sechdrs[i].sh_addr,
  545. sechdrs[i].sh_size);
  546. /* Update sh_addr to point to copy in image. */
  547. sechdrs[i].sh_addr = (unsigned long)dest;
  548. pr_debug(" section sh_name %s sh_addr 0x%x\n",
  549. secstrings + sechdrs[i].sh_name,
  550. sechdrs[i].sh_addr);
  551. }
  552. /* Fix up syms, so that st_value is a pointer to location. */
  553. simplify_symbols(sechdrs, symindex, strtab, secstrings,
  554. hdr->e_shnum, &mod);
  555. /* Now do relocations. */
  556. for (i = 1; i < hdr->e_shnum; i++) {
  557. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  558. unsigned int info = sechdrs[i].sh_info;
  559. /* Not a valid relocation section? */
  560. if (info >= hdr->e_shnum)
  561. continue;
  562. /* Don't bother with non-allocated sections */
  563. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  564. continue;
  565. if (sechdrs[i].sh_type == SHT_REL)
  566. err = apply_relocations(sechdrs, strtab,
  567. symindex, i, &mod);
  568. else if (sechdrs[i].sh_type == SHT_RELA)
  569. err = apply_relocate_add(sechdrs, strtab,
  570. symindex, i, &mod);
  571. if (err < 0)
  572. return err;
  573. }
  574. } else {
  575. struct elf_phdr *phdr = (struct elf_phdr *)
  576. ((char *)hdr + hdr->e_phoff);
  577. for (i = 0; i < hdr->e_phnum; i++) {
  578. if (phdr->p_type == PT_LOAD) {
  579. memcpy((void *)phdr->p_paddr,
  580. (char *)hdr + phdr->p_offset,
  581. phdr->p_filesz);
  582. memset((void *)phdr->p_paddr + phdr->p_filesz,
  583. 0, phdr->p_memsz - phdr->p_filesz);
  584. }
  585. phdr++;
  586. }
  587. for (i = 0; i < hdr->e_shnum; i++) {
  588. /* Internal symbols and strings. */
  589. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  590. symindex = i;
  591. strindex = sechdrs[i].sh_link;
  592. strtab = (char *)hdr +
  593. sechdrs[strindex].sh_offset;
  594. /*
  595. * mark symtab's address for when we try
  596. * to find the magic symbols
  597. */
  598. sechdrs[i].sh_addr = (size_t) hdr +
  599. sechdrs[i].sh_offset;
  600. }
  601. }
  602. }
  603. /* make sure it's physically written out */
  604. flush_icache_range((unsigned long)v->load_addr,
  605. (unsigned long)v->load_addr + v->len);
  606. if ((find_vpe_symbols(v, sechdrs, symindex, strtab, &mod)) < 0) {
  607. if (v->__start == 0) {
  608. pr_warn("VPE loader: program does not contain a __start symbol\n");
  609. return -ENOEXEC;
  610. }
  611. if (v->shared_ptr == NULL)
  612. pr_warn("VPE loader: program does not contain vpe_shared symbol.\n"
  613. " Unable to use AMVP (AP/SP) facilities.\n");
  614. }
  615. pr_info(" elf loaded\n");
  616. return 0;
  617. }
  618. static int getcwd(char *buff, int size)
  619. {
  620. mm_segment_t old_fs;
  621. int ret;
  622. old_fs = get_fs();
  623. set_fs(KERNEL_DS);
  624. ret = sys_getcwd(buff, size);
  625. set_fs(old_fs);
  626. return ret;
  627. }
  628. /* checks VPE is unused and gets ready to load program */
  629. static int vpe_open(struct inode *inode, struct file *filp)
  630. {
  631. enum vpe_state state;
  632. struct vpe_notifications *notifier;
  633. struct vpe *v;
  634. int ret;
  635. if (VPE_MODULE_MINOR != iminor(inode)) {
  636. /* assume only 1 device at the moment. */
  637. pr_warn("VPE loader: only vpe1 is supported\n");
  638. return -ENODEV;
  639. }
  640. v = get_vpe(aprp_cpu_index());
  641. if (v == NULL) {
  642. pr_warn("VPE loader: unable to get vpe\n");
  643. return -ENODEV;
  644. }
  645. state = xchg(&v->state, VPE_STATE_INUSE);
  646. if (state != VPE_STATE_UNUSED) {
  647. pr_debug("VPE loader: tc in use dumping regs\n");
  648. list_for_each_entry(notifier, &v->notify, list)
  649. notifier->stop(aprp_cpu_index());
  650. release_progmem(v->load_addr);
  651. cleanup_tc(get_tc(aprp_cpu_index()));
  652. }
  653. /* this of-course trashes what was there before... */
  654. v->pbuffer = vmalloc(P_SIZE);
  655. if (!v->pbuffer) {
  656. pr_warn("VPE loader: unable to allocate memory\n");
  657. return -ENOMEM;
  658. }
  659. v->plen = P_SIZE;
  660. v->load_addr = NULL;
  661. v->len = 0;
  662. v->cwd[0] = 0;
  663. ret = getcwd(v->cwd, VPE_PATH_MAX);
  664. if (ret < 0)
  665. pr_warn("VPE loader: open, getcwd returned %d\n", ret);
  666. v->shared_ptr = NULL;
  667. v->__start = 0;
  668. return 0;
  669. }
  670. static int vpe_release(struct inode *inode, struct file *filp)
  671. {
  672. #if defined(CONFIG_MIPS_VPE_LOADER_MT) || defined(CONFIG_MIPS_VPE_LOADER_CMP)
  673. struct vpe *v;
  674. Elf_Ehdr *hdr;
  675. int ret = 0;
  676. v = get_vpe(aprp_cpu_index());
  677. if (v == NULL)
  678. return -ENODEV;
  679. hdr = (Elf_Ehdr *) v->pbuffer;
  680. if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) == 0) {
  681. if (vpe_elfload(v) >= 0) {
  682. vpe_run(v);
  683. } else {
  684. pr_warn("VPE loader: ELF load failed.\n");
  685. ret = -ENOEXEC;
  686. }
  687. } else {
  688. pr_warn("VPE loader: only elf files are supported\n");
  689. ret = -ENOEXEC;
  690. }
  691. /* It's good to be able to run the SP and if it chokes have a look at
  692. the /dev/rt?. But if we reset the pointer to the shared struct we
  693. lose what has happened. So perhaps if garbage is sent to the vpe
  694. device, use it as a trigger for the reset. Hopefully a nice
  695. executable will be along shortly. */
  696. if (ret < 0)
  697. v->shared_ptr = NULL;
  698. vfree(v->pbuffer);
  699. v->plen = 0;
  700. return ret;
  701. #else
  702. pr_warn("VPE loader: ELF load failed.\n");
  703. return -ENOEXEC;
  704. #endif
  705. }
  706. static ssize_t vpe_write(struct file *file, const char __user *buffer,
  707. size_t count, loff_t *ppos)
  708. {
  709. size_t ret = count;
  710. struct vpe *v;
  711. if (iminor(file_inode(file)) != VPE_MODULE_MINOR)
  712. return -ENODEV;
  713. v = get_vpe(aprp_cpu_index());
  714. if (v == NULL)
  715. return -ENODEV;
  716. if ((count + v->len) > v->plen) {
  717. pr_warn("VPE loader: elf size too big. Perhaps strip uneeded symbols\n");
  718. return -ENOMEM;
  719. }
  720. count -= copy_from_user(v->pbuffer + v->len, buffer, count);
  721. if (!count)
  722. return -EFAULT;
  723. v->len += count;
  724. return ret;
  725. }
  726. const struct file_operations vpe_fops = {
  727. .owner = THIS_MODULE,
  728. .open = vpe_open,
  729. .release = vpe_release,
  730. .write = vpe_write,
  731. .llseek = noop_llseek,
  732. };
  733. void *vpe_get_shared(int index)
  734. {
  735. struct vpe *v = get_vpe(index);
  736. if (v == NULL)
  737. return NULL;
  738. return v->shared_ptr;
  739. }
  740. EXPORT_SYMBOL(vpe_get_shared);
  741. int vpe_notify(int index, struct vpe_notifications *notify)
  742. {
  743. struct vpe *v = get_vpe(index);
  744. if (v == NULL)
  745. return -1;
  746. list_add(&notify->list, &v->notify);
  747. return 0;
  748. }
  749. EXPORT_SYMBOL(vpe_notify);
  750. char *vpe_getcwd(int index)
  751. {
  752. struct vpe *v = get_vpe(index);
  753. if (v == NULL)
  754. return NULL;
  755. return v->cwd;
  756. }
  757. EXPORT_SYMBOL(vpe_getcwd);
  758. module_init(vpe_module_init);
  759. module_exit(vpe_module_exit);
  760. MODULE_DESCRIPTION("MIPS VPE Loader");
  761. MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
  762. MODULE_LICENSE("GPL");