vpe.c 30 KB

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