vpe.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /*
  2. * Copyright (C) 2004, 2005 MIPS Technologies, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can distribute it and/or modify it
  5. * under the terms of the GNU General Public License (Version 2) as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. * for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  16. *
  17. */
  18. /*
  19. * VPE support module
  20. *
  21. * Provides support for loading a MIPS SP program on VPE1.
  22. * The SP enviroment is rather simple, no tlb's. It needs to be relocatable
  23. * (or partially linked). You should initialise your stack in the startup
  24. * code. This loader looks for the symbol __start and sets up
  25. * execution to resume from there. The MIPS SDE kit contains suitable examples.
  26. *
  27. * To load and run, simply cat a SP 'program file' to /dev/vpe1.
  28. * i.e cat spapp >/dev/vpe1.
  29. *
  30. * You'll need to have the following device files.
  31. * mknod /dev/vpe0 c 63 0
  32. * mknod /dev/vpe1 c 63 1
  33. */
  34. #include <linux/config.h>
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/fs.h>
  38. #include <linux/init.h>
  39. #include <asm/uaccess.h>
  40. #include <linux/slab.h>
  41. #include <linux/list.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/elf.h>
  44. #include <linux/seq_file.h>
  45. #include <linux/syscalls.h>
  46. #include <linux/moduleloader.h>
  47. #include <linux/interrupt.h>
  48. #include <linux/poll.h>
  49. #include <linux/bootmem.h>
  50. #include <asm/mipsregs.h>
  51. #include <asm/mipsmtregs.h>
  52. #include <asm/cacheflush.h>
  53. #include <asm/atomic.h>
  54. #include <asm/cpu.h>
  55. #include <asm/processor.h>
  56. #include <asm/system.h>
  57. typedef void *vpe_handle;
  58. #ifndef ARCH_SHF_SMALL
  59. #define ARCH_SHF_SMALL 0
  60. #endif
  61. /* If this is set, the section belongs in the init part of the module */
  62. #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
  63. static char module_name[] = "vpe";
  64. static int major;
  65. /* grab the likely amount of memory we will need. */
  66. #ifdef CONFIG_MIPS_VPE_LOADER_TOM
  67. #define P_SIZE (2 * 1024 * 1024)
  68. #else
  69. /* add an overhead to the max kmalloc size for non-striped symbols/etc */
  70. #define P_SIZE (256 * 1024)
  71. #endif
  72. #define MAX_VPES 16
  73. enum vpe_state {
  74. VPE_STATE_UNUSED = 0,
  75. VPE_STATE_INUSE,
  76. VPE_STATE_RUNNING
  77. };
  78. enum tc_state {
  79. TC_STATE_UNUSED = 0,
  80. TC_STATE_INUSE,
  81. TC_STATE_RUNNING,
  82. TC_STATE_DYNAMIC
  83. };
  84. struct vpe {
  85. enum vpe_state state;
  86. /* (device) minor associated with this vpe */
  87. int minor;
  88. /* elfloader stuff */
  89. void *load_addr;
  90. unsigned long len;
  91. char *pbuffer;
  92. unsigned long plen;
  93. unsigned long __start;
  94. /* tc's associated with this vpe */
  95. struct list_head tc;
  96. /* The list of vpe's */
  97. struct list_head list;
  98. /* shared symbol address */
  99. void *shared_ptr;
  100. };
  101. struct tc {
  102. enum tc_state state;
  103. int index;
  104. /* parent VPE */
  105. struct vpe *pvpe;
  106. /* The list of TC's with this VPE */
  107. struct list_head tc;
  108. /* The global list of tc's */
  109. struct list_head list;
  110. };
  111. struct vpecontrol_ {
  112. /* Virtual processing elements */
  113. struct list_head vpe_list;
  114. /* Thread contexts */
  115. struct list_head tc_list;
  116. } vpecontrol;
  117. static void release_progmem(void *ptr);
  118. static void dump_vpe(struct vpe * v);
  119. extern void save_gp_address(unsigned int secbase, unsigned int rel);
  120. /* get the vpe associated with this minor */
  121. struct vpe *get_vpe(int minor)
  122. {
  123. struct vpe *v;
  124. list_for_each_entry(v, &vpecontrol.vpe_list, list) {
  125. if (v->minor == minor)
  126. return v;
  127. }
  128. printk(KERN_DEBUG "VPE: get_vpe minor %d not found\n", minor);
  129. return NULL;
  130. }
  131. /* get the vpe associated with this minor */
  132. struct tc *get_tc(int index)
  133. {
  134. struct tc *t;
  135. list_for_each_entry(t, &vpecontrol.tc_list, list) {
  136. if (t->index == index)
  137. return t;
  138. }
  139. printk(KERN_DEBUG "VPE: get_tc index %d not found\n", index);
  140. return NULL;
  141. }
  142. struct tc *get_tc_unused(void)
  143. {
  144. struct tc *t;
  145. list_for_each_entry(t, &vpecontrol.tc_list, list) {
  146. if (t->state == TC_STATE_UNUSED)
  147. return t;
  148. }
  149. printk(KERN_DEBUG "VPE: All TC's are in use\n");
  150. return NULL;
  151. }
  152. /* allocate a vpe and associate it with this minor (or index) */
  153. struct vpe *alloc_vpe(int minor)
  154. {
  155. struct vpe *v;
  156. if ((v = kzalloc(sizeof(struct vpe), GFP_KERNEL)) == NULL) {
  157. printk(KERN_WARNING "VPE: alloc_vpe no mem\n");
  158. return NULL;
  159. }
  160. INIT_LIST_HEAD(&v->tc);
  161. list_add_tail(&v->list, &vpecontrol.vpe_list);
  162. v->minor = minor;
  163. return v;
  164. }
  165. /* allocate a tc. At startup only tc0 is running, all other can be halted. */
  166. struct tc *alloc_tc(int index)
  167. {
  168. struct tc *t;
  169. if ((t = kzalloc(sizeof(struct tc), GFP_KERNEL)) == NULL) {
  170. printk(KERN_WARNING "VPE: alloc_tc no mem\n");
  171. return NULL;
  172. }
  173. INIT_LIST_HEAD(&t->tc);
  174. list_add_tail(&t->list, &vpecontrol.tc_list);
  175. t->index = index;
  176. return t;
  177. }
  178. /* clean up and free everything */
  179. void release_vpe(struct vpe *v)
  180. {
  181. list_del(&v->list);
  182. if (v->load_addr)
  183. release_progmem(v);
  184. kfree(v);
  185. }
  186. void dump_mtregs(void)
  187. {
  188. unsigned long val;
  189. val = read_c0_config3();
  190. printk("config3 0x%lx MT %ld\n", val,
  191. (val & CONFIG3_MT) >> CONFIG3_MT_SHIFT);
  192. val = read_c0_mvpconf0();
  193. printk("mvpconf0 0x%lx, PVPE %ld PTC %ld M %ld\n", val,
  194. (val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT,
  195. val & MVPCONF0_PTC, (val & MVPCONF0_M) >> MVPCONF0_M_SHIFT);
  196. val = read_c0_mvpcontrol();
  197. printk("MVPControl 0x%lx, STLB %ld VPC %ld EVP %ld\n", val,
  198. (val & MVPCONTROL_STLB) >> MVPCONTROL_STLB_SHIFT,
  199. (val & MVPCONTROL_VPC) >> MVPCONTROL_VPC_SHIFT,
  200. (val & MVPCONTROL_EVP));
  201. val = read_c0_vpeconf0();
  202. printk("VPEConf0 0x%lx MVP %ld\n", val,
  203. (val & VPECONF0_MVP) >> VPECONF0_MVP_SHIFT);
  204. }
  205. /* Find some VPE program space */
  206. static void *alloc_progmem(unsigned long len)
  207. {
  208. #ifdef CONFIG_MIPS_VPE_LOADER_TOM
  209. /* this means you must tell linux to use less memory than you physically have */
  210. return pfn_to_kaddr(max_pfn);
  211. #else
  212. // simple grab some mem for now
  213. return kmalloc(len, GFP_KERNEL);
  214. #endif
  215. }
  216. static void release_progmem(void *ptr)
  217. {
  218. #ifndef CONFIG_MIPS_VPE_LOADER_TOM
  219. kfree(ptr);
  220. #endif
  221. }
  222. /* Update size with this section: return offset. */
  223. static long get_offset(unsigned long *size, Elf_Shdr * sechdr)
  224. {
  225. long ret;
  226. ret = ALIGN(*size, sechdr->sh_addralign ? : 1);
  227. *size = ret + sechdr->sh_size;
  228. return ret;
  229. }
  230. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  231. might -- code, read-only data, read-write data, small data. Tally
  232. sizes, and place the offsets into sh_entsize fields: high bit means it
  233. belongs in init. */
  234. static void layout_sections(struct module *mod, const Elf_Ehdr * hdr,
  235. Elf_Shdr * sechdrs, const char *secstrings)
  236. {
  237. static unsigned long const masks[][2] = {
  238. /* NOTE: all executable code must be the first section
  239. * in this array; otherwise modify the text_size
  240. * finder in the two loops below */
  241. {SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL},
  242. {SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL},
  243. {SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL},
  244. {ARCH_SHF_SMALL | SHF_ALLOC, 0}
  245. };
  246. unsigned int m, i;
  247. for (i = 0; i < hdr->e_shnum; i++)
  248. sechdrs[i].sh_entsize = ~0UL;
  249. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  250. for (i = 0; i < hdr->e_shnum; ++i) {
  251. Elf_Shdr *s = &sechdrs[i];
  252. // || strncmp(secstrings + s->sh_name, ".init", 5) == 0)
  253. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  254. || (s->sh_flags & masks[m][1])
  255. || s->sh_entsize != ~0UL)
  256. continue;
  257. s->sh_entsize = get_offset(&mod->core_size, s);
  258. }
  259. if (m == 0)
  260. mod->core_text_size = mod->core_size;
  261. }
  262. }
  263. /* from module-elf32.c, but subverted a little */
  264. struct mips_hi16 {
  265. struct mips_hi16 *next;
  266. Elf32_Addr *addr;
  267. Elf32_Addr value;
  268. };
  269. static struct mips_hi16 *mips_hi16_list;
  270. static unsigned int gp_offs, gp_addr;
  271. static int apply_r_mips_none(struct module *me, uint32_t *location,
  272. Elf32_Addr v)
  273. {
  274. return 0;
  275. }
  276. static int apply_r_mips_gprel16(struct module *me, uint32_t *location,
  277. Elf32_Addr v)
  278. {
  279. int rel;
  280. if( !(*location & 0xffff) ) {
  281. rel = (int)v - gp_addr;
  282. }
  283. else {
  284. /* .sbss + gp(relative) + offset */
  285. /* kludge! */
  286. rel = (int)(short)((int)v + gp_offs +
  287. (int)(short)(*location & 0xffff) - gp_addr);
  288. }
  289. if( (rel > 32768) || (rel < -32768) ) {
  290. printk(KERN_ERR
  291. "apply_r_mips_gprel16: relative address out of range 0x%x %d\n",
  292. rel, rel);
  293. return -ENOEXEC;
  294. }
  295. *location = (*location & 0xffff0000) | (rel & 0xffff);
  296. return 0;
  297. }
  298. static int apply_r_mips_pc16(struct module *me, uint32_t *location,
  299. Elf32_Addr v)
  300. {
  301. int rel;
  302. rel = (((unsigned int)v - (unsigned int)location));
  303. rel >>= 2; // because the offset is in _instructions_ not bytes.
  304. rel -= 1; // and one instruction less due to the branch delay slot.
  305. if( (rel > 32768) || (rel < -32768) ) {
  306. printk(KERN_ERR
  307. "apply_r_mips_pc16: relative address out of range 0x%x\n", rel);
  308. return -ENOEXEC;
  309. }
  310. *location = (*location & 0xffff0000) | (rel & 0xffff);
  311. return 0;
  312. }
  313. static int apply_r_mips_32(struct module *me, uint32_t *location,
  314. Elf32_Addr v)
  315. {
  316. *location += v;
  317. return 0;
  318. }
  319. static int apply_r_mips_26(struct module *me, uint32_t *location,
  320. Elf32_Addr v)
  321. {
  322. if (v % 4) {
  323. printk(KERN_ERR "module %s: dangerous relocation mod4\n", me->name);
  324. return -ENOEXEC;
  325. }
  326. /*
  327. * Not desperately convinced this is a good check of an overflow condition
  328. * anyway. But it gets in the way of handling undefined weak symbols which
  329. * we want to set to zero.
  330. * if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  331. * printk(KERN_ERR
  332. * "module %s: relocation overflow\n",
  333. * me->name);
  334. * return -ENOEXEC;
  335. * }
  336. */
  337. *location = (*location & ~0x03ffffff) |
  338. ((*location + (v >> 2)) & 0x03ffffff);
  339. return 0;
  340. }
  341. static int apply_r_mips_hi16(struct module *me, uint32_t *location,
  342. Elf32_Addr v)
  343. {
  344. struct mips_hi16 *n;
  345. /*
  346. * We cannot relocate this one now because we don't know the value of
  347. * the carry we need to add. Save the information, and let LO16 do the
  348. * actual relocation.
  349. */
  350. n = kmalloc(sizeof *n, GFP_KERNEL);
  351. if (!n)
  352. return -ENOMEM;
  353. n->addr = location;
  354. n->value = v;
  355. n->next = mips_hi16_list;
  356. mips_hi16_list = n;
  357. return 0;
  358. }
  359. static int apply_r_mips_lo16(struct module *me, uint32_t *location,
  360. Elf32_Addr v)
  361. {
  362. unsigned long insnlo = *location;
  363. Elf32_Addr val, vallo;
  364. /* Sign extend the addend we extract from the lo insn. */
  365. vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
  366. if (mips_hi16_list != NULL) {
  367. struct mips_hi16 *l;
  368. l = mips_hi16_list;
  369. while (l != NULL) {
  370. struct mips_hi16 *next;
  371. unsigned long insn;
  372. /*
  373. * The value for the HI16 had best be the same.
  374. */
  375. if (v != l->value) {
  376. printk("%d != %d\n", v, l->value);
  377. goto out_danger;
  378. }
  379. /*
  380. * Do the HI16 relocation. Note that we actually don't
  381. * need to know anything about the LO16 itself, except
  382. * where to find the low 16 bits of the addend needed
  383. * by the LO16.
  384. */
  385. insn = *l->addr;
  386. val = ((insn & 0xffff) << 16) + vallo;
  387. val += v;
  388. /*
  389. * Account for the sign extension that will happen in
  390. * the low bits.
  391. */
  392. val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
  393. insn = (insn & ~0xffff) | val;
  394. *l->addr = insn;
  395. next = l->next;
  396. kfree(l);
  397. l = next;
  398. }
  399. mips_hi16_list = NULL;
  400. }
  401. /*
  402. * Ok, we're done with the HI16 relocs. Now deal with the LO16.
  403. */
  404. val = v + vallo;
  405. insnlo = (insnlo & ~0xffff) | (val & 0xffff);
  406. *location = insnlo;
  407. return 0;
  408. out_danger:
  409. printk(KERN_ERR "module %s: dangerous " "relocation\n", me->name);
  410. return -ENOEXEC;
  411. }
  412. static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
  413. Elf32_Addr v) = {
  414. [R_MIPS_NONE] = apply_r_mips_none,
  415. [R_MIPS_32] = apply_r_mips_32,
  416. [R_MIPS_26] = apply_r_mips_26,
  417. [R_MIPS_HI16] = apply_r_mips_hi16,
  418. [R_MIPS_LO16] = apply_r_mips_lo16,
  419. [R_MIPS_GPREL16] = apply_r_mips_gprel16,
  420. [R_MIPS_PC16] = apply_r_mips_pc16
  421. };
  422. int apply_relocations(Elf32_Shdr *sechdrs,
  423. const char *strtab,
  424. unsigned int symindex,
  425. unsigned int relsec,
  426. struct module *me)
  427. {
  428. Elf32_Rel *rel = (void *) sechdrs[relsec].sh_addr;
  429. Elf32_Sym *sym;
  430. uint32_t *location;
  431. unsigned int i;
  432. Elf32_Addr v;
  433. int res;
  434. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  435. Elf32_Word r_info = rel[i].r_info;
  436. /* This is where to make the change */
  437. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  438. + rel[i].r_offset;
  439. /* This is the symbol it is referring to */
  440. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  441. + ELF32_R_SYM(r_info);
  442. if (!sym->st_value) {
  443. printk(KERN_DEBUG "%s: undefined weak symbol %s\n",
  444. me->name, strtab + sym->st_name);
  445. /* just print the warning, dont barf */
  446. }
  447. v = sym->st_value;
  448. res = reloc_handlers[ELF32_R_TYPE(r_info)](me, location, v);
  449. if( res ) {
  450. printk(KERN_DEBUG
  451. "relocation error 0x%x sym refer <%s> value 0x%x "
  452. "type 0x%x r_info 0x%x\n",
  453. (unsigned int)location, strtab + sym->st_name, v,
  454. r_info, ELF32_R_TYPE(r_info));
  455. }
  456. if (res)
  457. return res;
  458. }
  459. return 0;
  460. }
  461. void save_gp_address(unsigned int secbase, unsigned int rel)
  462. {
  463. gp_addr = secbase + rel;
  464. gp_offs = gp_addr - (secbase & 0xffff0000);
  465. }
  466. /* end module-elf32.c */
  467. /* Change all symbols so that sh_value encodes the pointer directly. */
  468. static int simplify_symbols(Elf_Shdr * sechdrs,
  469. unsigned int symindex,
  470. const char *strtab,
  471. const char *secstrings,
  472. unsigned int nsecs, struct module *mod)
  473. {
  474. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  475. unsigned long secbase, bssbase = 0;
  476. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  477. int ret = 0, size;
  478. /* find the .bss section for COMMON symbols */
  479. for (i = 0; i < nsecs; i++) {
  480. if (strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) == 0)
  481. bssbase = sechdrs[i].sh_addr;
  482. }
  483. for (i = 1; i < n; i++) {
  484. switch (sym[i].st_shndx) {
  485. case SHN_COMMON:
  486. /* Allocate space for the symbol in the .bss section. st_value is currently size.
  487. We want it to have the address of the symbol. */
  488. size = sym[i].st_value;
  489. sym[i].st_value = bssbase;
  490. bssbase += size;
  491. break;
  492. case SHN_ABS:
  493. /* Don't need to do anything */
  494. break;
  495. case SHN_UNDEF:
  496. /* ret = -ENOENT; */
  497. break;
  498. case SHN_MIPS_SCOMMON:
  499. printk(KERN_DEBUG
  500. "simplify_symbols: ignoring SHN_MIPS_SCOMMON symbol <%s> st_shndx %d\n",
  501. strtab + sym[i].st_name, sym[i].st_shndx);
  502. // .sbss section
  503. break;
  504. default:
  505. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  506. if (strncmp(strtab + sym[i].st_name, "_gp", 3) == 0) {
  507. save_gp_address(secbase, sym[i].st_value);
  508. }
  509. sym[i].st_value += secbase;
  510. break;
  511. }
  512. }
  513. return ret;
  514. }
  515. #ifdef DEBUG_ELFLOADER
  516. static void dump_elfsymbols(Elf_Shdr * sechdrs, unsigned int symindex,
  517. const char *strtab, struct module *mod)
  518. {
  519. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  520. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  521. printk(KERN_DEBUG "dump_elfsymbols: n %d\n", n);
  522. for (i = 1; i < n; i++) {
  523. printk(KERN_DEBUG " i %d name <%s> 0x%x\n", i,
  524. strtab + sym[i].st_name, sym[i].st_value);
  525. }
  526. }
  527. #endif
  528. static void dump_tc(struct tc *t)
  529. {
  530. printk(KERN_WARNING "VPE: TC index %d TCStatus 0x%lx halt 0x%lx\n",
  531. t->index, read_tc_c0_tcstatus(), read_tc_c0_tchalt());
  532. printk(KERN_WARNING "VPE: tcrestart 0x%lx\n", read_tc_c0_tcrestart());
  533. }
  534. static void dump_tclist(void)
  535. {
  536. struct tc *t;
  537. list_for_each_entry(t, &vpecontrol.tc_list, list) {
  538. dump_tc(t);
  539. }
  540. }
  541. /* We are prepared so configure and start the VPE... */
  542. int vpe_run(struct vpe * v)
  543. {
  544. unsigned long val;
  545. struct tc *t;
  546. /* check we are the Master VPE */
  547. val = read_c0_vpeconf0();
  548. if (!(val & VPECONF0_MVP)) {
  549. printk(KERN_WARNING
  550. "VPE: only Master VPE's are allowed to configure MT\n");
  551. return -1;
  552. }
  553. /* disable MT (using dvpe) */
  554. dvpe();
  555. /* Put MVPE's into 'configuration state' */
  556. set_c0_mvpcontrol(MVPCONTROL_VPC);
  557. if (!list_empty(&v->tc)) {
  558. if ((t = list_entry(v->tc.next, struct tc, tc)) == NULL) {
  559. printk(KERN_WARNING "VPE: TC %d is already in use.\n",
  560. t->index);
  561. return -ENOEXEC;
  562. }
  563. } else {
  564. printk(KERN_WARNING "VPE: No TC's associated with VPE %d\n",
  565. v->minor);
  566. return -ENOEXEC;
  567. }
  568. settc(t->index);
  569. val = read_vpe_c0_vpeconf0();
  570. /* should check it is halted, and not activated */
  571. if ((read_tc_c0_tcstatus() & TCSTATUS_A) || !(read_tc_c0_tchalt() & TCHALT_H)) {
  572. printk(KERN_WARNING "VPE: TC %d is already doing something!\n",
  573. t->index);
  574. dump_tclist();
  575. return -ENOEXEC;
  576. }
  577. /* Write the address we want it to start running from in the TCPC register. */
  578. write_tc_c0_tcrestart((unsigned long)v->__start);
  579. /* write the sivc_info address to tccontext */
  580. write_tc_c0_tccontext((unsigned long)0);
  581. /* Set up the XTC bit in vpeconf0 to point at our tc */
  582. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | (t->index << VPECONF0_XTC_SHIFT));
  583. /* mark the TC as activated, not interrupt exempt and not dynamically allocatable */
  584. val = read_tc_c0_tcstatus();
  585. val = (val & ~(TCSTATUS_DA | TCSTATUS_IXMT)) | TCSTATUS_A;
  586. write_tc_c0_tcstatus(val);
  587. write_tc_c0_tchalt(read_tc_c0_tchalt() & ~TCHALT_H);
  588. /* set up VPE1 */
  589. write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() & ~VPECONTROL_TE); // no multiple TC's
  590. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA); // enable this VPE
  591. /*
  592. * The sde-kit passes 'memsize' to __start in $a3, so set something
  593. * here...
  594. * Or set $a3 (register 7) to zero and define DFLT_STACK_SIZE and
  595. * DFLT_HEAP_SIZE when you compile your program
  596. */
  597. mttgpr(7, 0);
  598. /* set config to be the same as vpe0, particularly kseg0 coherency alg */
  599. write_vpe_c0_config(read_c0_config());
  600. /* clear out any left overs from a previous program */
  601. write_vpe_c0_cause(0);
  602. /* take system out of configuration state */
  603. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  604. /* clear interrupts enabled IE, ERL, EXL, and KSU from c0 status */
  605. write_vpe_c0_status(read_vpe_c0_status() & ~(ST0_ERL | ST0_KSU | ST0_IE | ST0_EXL));
  606. /* set it running */
  607. evpe(EVPE_ENABLE);
  608. return 0;
  609. }
  610. static unsigned long find_vpe_symbols(struct vpe * v, Elf_Shdr * sechdrs,
  611. unsigned int symindex, const char *strtab,
  612. struct module *mod)
  613. {
  614. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  615. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  616. for (i = 1; i < n; i++) {
  617. if (strcmp(strtab + sym[i].st_name, "__start") == 0) {
  618. v->__start = sym[i].st_value;
  619. }
  620. if (strcmp(strtab + sym[i].st_name, "vpe_shared") == 0) {
  621. v->shared_ptr = (void *)sym[i].st_value;
  622. }
  623. }
  624. return 0;
  625. }
  626. /*
  627. * Allocates a VPE with some program code space(the load address), copies
  628. * the contents of the program (p)buffer performing relocatations/etc,
  629. * free's it when finished.
  630. */
  631. int vpe_elfload(struct vpe * v)
  632. {
  633. Elf_Ehdr *hdr;
  634. Elf_Shdr *sechdrs;
  635. long err = 0;
  636. char *secstrings, *strtab = NULL;
  637. unsigned int len, i, symindex = 0, strindex = 0;
  638. struct module mod; // so we can re-use the relocations code
  639. memset(&mod, 0, sizeof(struct module));
  640. strcpy(mod.name, "VPE dummy prog module");
  641. hdr = (Elf_Ehdr *) v->pbuffer;
  642. len = v->plen;
  643. /* Sanity checks against insmoding binaries or wrong arch,
  644. weird elf version */
  645. if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
  646. || hdr->e_type != ET_REL || !elf_check_arch(hdr)
  647. || hdr->e_shentsize != sizeof(*sechdrs)) {
  648. printk(KERN_WARNING
  649. "VPE program, wrong arch or weird elf version\n");
  650. return -ENOEXEC;
  651. }
  652. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
  653. printk(KERN_ERR "VPE program length %u truncated\n", len);
  654. return -ENOEXEC;
  655. }
  656. /* Convenience variables */
  657. sechdrs = (void *)hdr + hdr->e_shoff;
  658. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  659. sechdrs[0].sh_addr = 0;
  660. /* And these should exist, but gcc whinges if we don't init them */
  661. symindex = strindex = 0;
  662. for (i = 1; i < hdr->e_shnum; i++) {
  663. if (sechdrs[i].sh_type != SHT_NOBITS
  664. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size) {
  665. printk(KERN_ERR "VPE program length %u truncated\n",
  666. len);
  667. return -ENOEXEC;
  668. }
  669. /* Mark all sections sh_addr with their address in the
  670. temporary image. */
  671. sechdrs[i].sh_addr = (size_t) hdr + sechdrs[i].sh_offset;
  672. /* Internal symbols and strings. */
  673. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  674. symindex = i;
  675. strindex = sechdrs[i].sh_link;
  676. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  677. }
  678. }
  679. layout_sections(&mod, hdr, sechdrs, secstrings);
  680. v->load_addr = alloc_progmem(mod.core_size);
  681. memset(v->load_addr, 0, mod.core_size);
  682. printk("VPE elf_loader: loading to %p\n", v->load_addr);
  683. for (i = 0; i < hdr->e_shnum; i++) {
  684. void *dest;
  685. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  686. continue;
  687. dest = v->load_addr + sechdrs[i].sh_entsize;
  688. if (sechdrs[i].sh_type != SHT_NOBITS)
  689. memcpy(dest, (void *)sechdrs[i].sh_addr,
  690. sechdrs[i].sh_size);
  691. /* Update sh_addr to point to copy in image. */
  692. sechdrs[i].sh_addr = (unsigned long)dest;
  693. }
  694. /* Fix up syms, so that st_value is a pointer to location. */
  695. err =
  696. simplify_symbols(sechdrs, symindex, strtab, secstrings,
  697. hdr->e_shnum, &mod);
  698. if (err < 0) {
  699. printk(KERN_WARNING "VPE: unable to simplify symbols\n");
  700. goto cleanup;
  701. }
  702. /* Now do relocations. */
  703. for (i = 1; i < hdr->e_shnum; i++) {
  704. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  705. unsigned int info = sechdrs[i].sh_info;
  706. /* Not a valid relocation section? */
  707. if (info >= hdr->e_shnum)
  708. continue;
  709. /* Don't bother with non-allocated sections */
  710. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  711. continue;
  712. if (sechdrs[i].sh_type == SHT_REL)
  713. err =
  714. apply_relocations(sechdrs, strtab, symindex, i, &mod);
  715. else if (sechdrs[i].sh_type == SHT_RELA)
  716. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  717. &mod);
  718. if (err < 0) {
  719. printk(KERN_WARNING
  720. "vpe_elfload: error in relocations err %ld\n",
  721. err);
  722. goto cleanup;
  723. }
  724. }
  725. /* make sure it's physically written out */
  726. flush_icache_range((unsigned long)v->load_addr,
  727. (unsigned long)v->load_addr + v->len);
  728. if ((find_vpe_symbols(v, sechdrs, symindex, strtab, &mod)) < 0) {
  729. printk(KERN_WARNING
  730. "VPE: program doesn't contain __start or vpe_shared symbols\n");
  731. err = -ENOEXEC;
  732. }
  733. printk(" elf loaded\n");
  734. cleanup:
  735. return err;
  736. }
  737. static void dump_vpe(struct vpe * v)
  738. {
  739. struct tc *t;
  740. printk(KERN_DEBUG "VPEControl 0x%lx\n", read_vpe_c0_vpecontrol());
  741. printk(KERN_DEBUG "VPEConf0 0x%lx\n", read_vpe_c0_vpeconf0());
  742. list_for_each_entry(t, &vpecontrol.tc_list, list) {
  743. dump_tc(t);
  744. }
  745. }
  746. /* checks for VPE is unused and gets ready to load program */
  747. static int vpe_open(struct inode *inode, struct file *filp)
  748. {
  749. int minor;
  750. struct vpe *v;
  751. /* assume only 1 device at the mo. */
  752. if ((minor = MINOR(inode->i_rdev)) != 1) {
  753. printk(KERN_WARNING "VPE: only vpe1 is supported\n");
  754. return -ENODEV;
  755. }
  756. if ((v = get_vpe(minor)) == NULL) {
  757. printk(KERN_WARNING "VPE: unable to get vpe\n");
  758. return -ENODEV;
  759. }
  760. if (v->state != VPE_STATE_UNUSED) {
  761. unsigned long tmp;
  762. struct tc *t;
  763. printk(KERN_WARNING "VPE: device %d already in use\n", minor);
  764. dvpe();
  765. dump_vpe(v);
  766. printk(KERN_WARNING "VPE: re-initialising %d\n", minor);
  767. release_progmem(v->load_addr);
  768. t = get_tc(minor);
  769. settc(minor);
  770. tmp = read_tc_c0_tcstatus();
  771. /* mark not allocated and not dynamically allocatable */
  772. tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
  773. tmp |= TCSTATUS_IXMT; /* interrupt exempt */
  774. write_tc_c0_tcstatus(tmp);
  775. write_tc_c0_tchalt(TCHALT_H);
  776. }
  777. // allocate it so when we get write ops we know it's expected.
  778. v->state = VPE_STATE_INUSE;
  779. /* this of-course trashes what was there before... */
  780. v->pbuffer = vmalloc(P_SIZE);
  781. v->plen = P_SIZE;
  782. v->load_addr = NULL;
  783. v->len = 0;
  784. return 0;
  785. }
  786. static int vpe_release(struct inode *inode, struct file *filp)
  787. {
  788. int minor, ret = 0;
  789. struct vpe *v;
  790. Elf_Ehdr *hdr;
  791. minor = MINOR(inode->i_rdev);
  792. if ((v = get_vpe(minor)) == NULL)
  793. return -ENODEV;
  794. // simple case of fire and forget, so tell the VPE to run...
  795. hdr = (Elf_Ehdr *) v->pbuffer;
  796. if (memcmp(hdr->e_ident, ELFMAG, 4) == 0) {
  797. if (vpe_elfload(v) >= 0)
  798. vpe_run(v);
  799. else {
  800. printk(KERN_WARNING "VPE: ELF load failed.\n");
  801. ret = -ENOEXEC;
  802. }
  803. } else {
  804. printk(KERN_WARNING "VPE: only elf files are supported\n");
  805. ret = -ENOEXEC;
  806. }
  807. // cleanup any temp buffers
  808. if (v->pbuffer)
  809. vfree(v->pbuffer);
  810. v->plen = 0;
  811. return ret;
  812. }
  813. static ssize_t vpe_write(struct file *file, const char __user * buffer,
  814. size_t count, loff_t * ppos)
  815. {
  816. int minor;
  817. size_t ret = count;
  818. struct vpe *v;
  819. minor = MINOR(file->f_dentry->d_inode->i_rdev);
  820. if ((v = get_vpe(minor)) == NULL)
  821. return -ENODEV;
  822. if (v->pbuffer == NULL) {
  823. printk(KERN_ERR "vpe_write: no pbuffer\n");
  824. return -ENOMEM;
  825. }
  826. if ((count + v->len) > v->plen) {
  827. printk(KERN_WARNING
  828. "VPE Loader: elf size too big. Perhaps strip uneeded symbols\n");
  829. return -ENOMEM;
  830. }
  831. count -= copy_from_user(v->pbuffer + v->len, buffer, count);
  832. if (!count) {
  833. printk("vpe_write: copy_to_user failed\n");
  834. return -EFAULT;
  835. }
  836. v->len += count;
  837. return ret;
  838. }
  839. static struct file_operations vpe_fops = {
  840. .owner = THIS_MODULE,
  841. .open = vpe_open,
  842. .release = vpe_release,
  843. .write = vpe_write
  844. };
  845. /* module wrapper entry points */
  846. /* give me a vpe */
  847. vpe_handle vpe_alloc(void)
  848. {
  849. int i;
  850. struct vpe *v;
  851. /* find a vpe */
  852. for (i = 1; i < MAX_VPES; i++) {
  853. if ((v = get_vpe(i)) != NULL) {
  854. v->state = VPE_STATE_INUSE;
  855. return v;
  856. }
  857. }
  858. return NULL;
  859. }
  860. EXPORT_SYMBOL(vpe_alloc);
  861. /* start running from here */
  862. int vpe_start(vpe_handle vpe, unsigned long start)
  863. {
  864. struct vpe *v = vpe;
  865. v->__start = start;
  866. return vpe_run(v);
  867. }
  868. EXPORT_SYMBOL(vpe_start);
  869. /* halt it for now */
  870. int vpe_stop(vpe_handle vpe)
  871. {
  872. struct vpe *v = vpe;
  873. struct tc *t;
  874. unsigned int evpe_flags;
  875. evpe_flags = dvpe();
  876. if ((t = list_entry(v->tc.next, struct tc, tc)) != NULL) {
  877. settc(t->index);
  878. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~VPECONF0_VPA);
  879. }
  880. evpe(evpe_flags);
  881. return 0;
  882. }
  883. EXPORT_SYMBOL(vpe_stop);
  884. /* I've done with it thank you */
  885. int vpe_free(vpe_handle vpe)
  886. {
  887. struct vpe *v = vpe;
  888. struct tc *t;
  889. unsigned int evpe_flags;
  890. if ((t = list_entry(v->tc.next, struct tc, tc)) == NULL) {
  891. return -ENOEXEC;
  892. }
  893. evpe_flags = dvpe();
  894. /* Put MVPE's into 'configuration state' */
  895. set_c0_mvpcontrol(MVPCONTROL_VPC);
  896. settc(t->index);
  897. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~VPECONF0_VPA);
  898. /* mark the TC unallocated and halt'ed */
  899. write_tc_c0_tcstatus(read_tc_c0_tcstatus() & ~TCSTATUS_A);
  900. write_tc_c0_tchalt(TCHALT_H);
  901. v->state = VPE_STATE_UNUSED;
  902. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  903. evpe(evpe_flags);
  904. return 0;
  905. }
  906. EXPORT_SYMBOL(vpe_free);
  907. void *vpe_get_shared(int index)
  908. {
  909. struct vpe *v;
  910. if ((v = get_vpe(index)) == NULL) {
  911. printk(KERN_WARNING "vpe: invalid vpe index %d\n", index);
  912. return NULL;
  913. }
  914. return v->shared_ptr;
  915. }
  916. EXPORT_SYMBOL(vpe_get_shared);
  917. static int __init vpe_module_init(void)
  918. {
  919. struct vpe *v = NULL;
  920. struct tc *t;
  921. unsigned long val;
  922. int i;
  923. if (!cpu_has_mipsmt) {
  924. printk("VPE loader: not a MIPS MT capable processor\n");
  925. return -ENODEV;
  926. }
  927. major = register_chrdev(0, module_name, &vpe_fops);
  928. if (major < 0) {
  929. printk("VPE loader: unable to register character device\n");
  930. return major;
  931. }
  932. dmt();
  933. dvpe();
  934. /* Put MVPE's into 'configuration state' */
  935. set_c0_mvpcontrol(MVPCONTROL_VPC);
  936. /* dump_mtregs(); */
  937. INIT_LIST_HEAD(&vpecontrol.vpe_list);
  938. INIT_LIST_HEAD(&vpecontrol.tc_list);
  939. val = read_c0_mvpconf0();
  940. for (i = 0; i < ((val & MVPCONF0_PTC) + 1); i++) {
  941. t = alloc_tc(i);
  942. /* VPE's */
  943. if (i < ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1) {
  944. settc(i);
  945. if ((v = alloc_vpe(i)) == NULL) {
  946. printk(KERN_WARNING "VPE: unable to allocate VPE\n");
  947. return -ENODEV;
  948. }
  949. list_add(&t->tc, &v->tc); /* add the tc to the list of this vpe's tc's. */
  950. /* deactivate all but vpe0 */
  951. if (i != 0) {
  952. unsigned long tmp = read_vpe_c0_vpeconf0();
  953. tmp &= ~VPECONF0_VPA;
  954. /* master VPE */
  955. tmp |= VPECONF0_MVP;
  956. write_vpe_c0_vpeconf0(tmp);
  957. }
  958. /* disable multi-threading with TC's */
  959. write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() & ~VPECONTROL_TE);
  960. if (i != 0) {
  961. write_vpe_c0_status((read_c0_status() &
  962. ~(ST0_IM | ST0_IE | ST0_KSU))
  963. | ST0_CU0);
  964. /* set config to be the same as vpe0, particularly kseg0 coherency alg */
  965. write_vpe_c0_config(read_c0_config());
  966. }
  967. }
  968. /* TC's */
  969. t->pvpe = v; /* set the parent vpe */
  970. if (i != 0) {
  971. unsigned long tmp;
  972. /* tc 0 will of course be running.... */
  973. if (i == 0)
  974. t->state = TC_STATE_RUNNING;
  975. settc(i);
  976. /* bind a TC to each VPE, May as well put all excess TC's
  977. on the last VPE */
  978. if (i >= (((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1))
  979. write_tc_c0_tcbind(read_tc_c0_tcbind() |
  980. ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT));
  981. else
  982. write_tc_c0_tcbind(read_tc_c0_tcbind() | i);
  983. tmp = read_tc_c0_tcstatus();
  984. /* mark not allocated and not dynamically allocatable */
  985. tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
  986. tmp |= TCSTATUS_IXMT; /* interrupt exempt */
  987. write_tc_c0_tcstatus(tmp);
  988. write_tc_c0_tchalt(TCHALT_H);
  989. }
  990. }
  991. /* release config state */
  992. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  993. return 0;
  994. }
  995. static void __exit vpe_module_exit(void)
  996. {
  997. struct vpe *v, *n;
  998. list_for_each_entry_safe(v, n, &vpecontrol.vpe_list, list) {
  999. if (v->state != VPE_STATE_UNUSED) {
  1000. release_vpe(v);
  1001. }
  1002. }
  1003. unregister_chrdev(major, module_name);
  1004. }
  1005. module_init(vpe_module_init);
  1006. module_exit(vpe_module_exit);
  1007. MODULE_DESCRIPTION("MIPS VPE Loader");
  1008. MODULE_AUTHOR("Elizabeth Clarke, MIPS Technologies, Inc");
  1009. MODULE_LICENSE("GPL");