vpe.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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. u32 len;
  91. char *pbuffer;
  92. u32 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(u32 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 (void *)((max_pfn * PAGE_SIZE) + KSEG0);
  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. if ((major = register_chrdev(0, module_name, &vpe_fops) < 0)) {
  928. printk("VPE loader: unable to register character device\n");
  929. return major;
  930. }
  931. dmt();
  932. dvpe();
  933. /* Put MVPE's into 'configuration state' */
  934. set_c0_mvpcontrol(MVPCONTROL_VPC);
  935. /* dump_mtregs(); */
  936. INIT_LIST_HEAD(&vpecontrol.vpe_list);
  937. INIT_LIST_HEAD(&vpecontrol.tc_list);
  938. val = read_c0_mvpconf0();
  939. for (i = 0; i < ((val & MVPCONF0_PTC) + 1); i++) {
  940. t = alloc_tc(i);
  941. /* VPE's */
  942. if (i < ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1) {
  943. settc(i);
  944. if ((v = alloc_vpe(i)) == NULL) {
  945. printk(KERN_WARNING "VPE: unable to allocate VPE\n");
  946. return -ENODEV;
  947. }
  948. list_add(&t->tc, &v->tc); /* add the tc to the list of this vpe's tc's. */
  949. /* deactivate all but vpe0 */
  950. if (i != 0) {
  951. unsigned long tmp = read_vpe_c0_vpeconf0();
  952. tmp &= ~VPECONF0_VPA;
  953. /* master VPE */
  954. tmp |= VPECONF0_MVP;
  955. write_vpe_c0_vpeconf0(tmp);
  956. }
  957. /* disable multi-threading with TC's */
  958. write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() & ~VPECONTROL_TE);
  959. if (i != 0) {
  960. write_vpe_c0_status((read_c0_status() &
  961. ~(ST0_IM | ST0_IE | ST0_KSU))
  962. | ST0_CU0);
  963. /* set config to be the same as vpe0, particularly kseg0 coherency alg */
  964. write_vpe_c0_config(read_c0_config());
  965. }
  966. }
  967. /* TC's */
  968. t->pvpe = v; /* set the parent vpe */
  969. if (i != 0) {
  970. unsigned long tmp;
  971. /* tc 0 will of course be running.... */
  972. if (i == 0)
  973. t->state = TC_STATE_RUNNING;
  974. settc(i);
  975. /* bind a TC to each VPE, May as well put all excess TC's
  976. on the last VPE */
  977. if (i >= (((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1))
  978. write_tc_c0_tcbind(read_tc_c0_tcbind() |
  979. ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT));
  980. else
  981. write_tc_c0_tcbind(read_tc_c0_tcbind() | i);
  982. tmp = read_tc_c0_tcstatus();
  983. /* mark not allocated and not dynamically allocatable */
  984. tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
  985. tmp |= TCSTATUS_IXMT; /* interrupt exempt */
  986. write_tc_c0_tcstatus(tmp);
  987. write_tc_c0_tchalt(TCHALT_H);
  988. }
  989. }
  990. /* release config state */
  991. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  992. return 0;
  993. }
  994. static void __exit vpe_module_exit(void)
  995. {
  996. struct vpe *v, *n;
  997. list_for_each_entry_safe(v, n, &vpecontrol.vpe_list, list) {
  998. if (v->state != VPE_STATE_UNUSED) {
  999. release_vpe(v);
  1000. }
  1001. }
  1002. unregister_chrdev(major, module_name);
  1003. }
  1004. module_init(vpe_module_init);
  1005. module_exit(vpe_module_exit);
  1006. MODULE_DESCRIPTION("MIPS VPE Loader");
  1007. MODULE_AUTHOR("Elizabeth Clarke, MIPS Technologies, Inc");
  1008. MODULE_LICENSE("GPL");