vpe.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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. * VPE support module
  19. *
  20. * Provides support for loading a MIPS SP program on VPE1.
  21. * The SP enviroment is rather simple, no tlb's. It needs to be relocatable
  22. * (or partially linked). You should initialise your stack in the startup
  23. * code. This loader looks for the symbol __start and sets up
  24. * execution to resume from there. The MIPS SDE kit contains suitable examples.
  25. *
  26. * To load and run, simply cat a SP 'program file' to /dev/vpe1.
  27. * i.e cat spapp >/dev/vpe1.
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/fs.h>
  32. #include <linux/init.h>
  33. #include <asm/uaccess.h>
  34. #include <linux/slab.h>
  35. #include <linux/list.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/elf.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/syscalls.h>
  40. #include <linux/moduleloader.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/poll.h>
  43. #include <linux/bootmem.h>
  44. #include <asm/mipsregs.h>
  45. #include <asm/mipsmtregs.h>
  46. #include <asm/cacheflush.h>
  47. #include <asm/atomic.h>
  48. #include <asm/cpu.h>
  49. #include <asm/processor.h>
  50. #include <asm/system.h>
  51. #include <asm/vpe.h>
  52. #include <asm/kspd.h>
  53. typedef void *vpe_handle;
  54. #ifndef ARCH_SHF_SMALL
  55. #define ARCH_SHF_SMALL 0
  56. #endif
  57. /* If this is set, the section belongs in the init part of the module */
  58. #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
  59. static char module_name[] = "vpe";
  60. static int major;
  61. #ifdef CONFIG_MIPS_APSP_KSPD
  62. static struct kspd_notifications kspd_events;
  63. static int kspd_events_reqd = 0;
  64. #endif
  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. extern unsigned long physical_memsize;
  73. #define MAX_VPES 16
  74. #define VPE_PATH_MAX 256
  75. enum vpe_state {
  76. VPE_STATE_UNUSED = 0,
  77. VPE_STATE_INUSE,
  78. VPE_STATE_RUNNING
  79. };
  80. enum tc_state {
  81. TC_STATE_UNUSED = 0,
  82. TC_STATE_INUSE,
  83. TC_STATE_RUNNING,
  84. TC_STATE_DYNAMIC
  85. };
  86. struct vpe {
  87. enum vpe_state state;
  88. /* (device) minor associated with this vpe */
  89. int minor;
  90. /* elfloader stuff */
  91. void *load_addr;
  92. unsigned long len;
  93. char *pbuffer;
  94. unsigned long plen;
  95. unsigned int uid, gid;
  96. char cwd[VPE_PATH_MAX];
  97. unsigned long __start;
  98. /* tc's associated with this vpe */
  99. struct list_head tc;
  100. /* The list of vpe's */
  101. struct list_head list;
  102. /* shared symbol address */
  103. void *shared_ptr;
  104. /* the list of who wants to know when something major happens */
  105. struct list_head notify;
  106. };
  107. struct tc {
  108. enum tc_state state;
  109. int index;
  110. /* parent VPE */
  111. struct vpe *pvpe;
  112. /* The list of TC's with this VPE */
  113. struct list_head tc;
  114. /* The global list of tc's */
  115. struct list_head list;
  116. };
  117. struct {
  118. /* Virtual processing elements */
  119. struct list_head vpe_list;
  120. /* Thread contexts */
  121. struct list_head tc_list;
  122. } vpecontrol = {
  123. .vpe_list = LIST_HEAD_INIT(vpecontrol.vpe_list),
  124. .tc_list = LIST_HEAD_INIT(vpecontrol.tc_list)
  125. };
  126. static void release_progmem(void *ptr);
  127. /* static __attribute_used__ void dump_vpe(struct vpe * v); */
  128. extern void save_gp_address(unsigned int secbase, unsigned int rel);
  129. /* get the vpe associated with this minor */
  130. struct vpe *get_vpe(int minor)
  131. {
  132. struct vpe *v;
  133. if (!cpu_has_mipsmt)
  134. return NULL;
  135. list_for_each_entry(v, &vpecontrol.vpe_list, list) {
  136. if (v->minor == minor)
  137. return v;
  138. }
  139. return NULL;
  140. }
  141. /* get the vpe associated with this minor */
  142. struct tc *get_tc(int index)
  143. {
  144. struct tc *t;
  145. list_for_each_entry(t, &vpecontrol.tc_list, list) {
  146. if (t->index == index)
  147. return t;
  148. }
  149. return NULL;
  150. }
  151. struct tc *get_tc_unused(void)
  152. {
  153. struct tc *t;
  154. list_for_each_entry(t, &vpecontrol.tc_list, list) {
  155. if (t->state == TC_STATE_UNUSED)
  156. return t;
  157. }
  158. return NULL;
  159. }
  160. /* allocate a vpe and associate it with this minor (or index) */
  161. struct vpe *alloc_vpe(int minor)
  162. {
  163. struct vpe *v;
  164. if ((v = kzalloc(sizeof(struct vpe), GFP_KERNEL)) == NULL) {
  165. return NULL;
  166. }
  167. INIT_LIST_HEAD(&v->tc);
  168. list_add_tail(&v->list, &vpecontrol.vpe_list);
  169. INIT_LIST_HEAD(&v->notify);
  170. v->minor = minor;
  171. return v;
  172. }
  173. /* allocate a tc. At startup only tc0 is running, all other can be halted. */
  174. struct tc *alloc_tc(int index)
  175. {
  176. struct tc *t;
  177. if ((t = kzalloc(sizeof(struct tc), GFP_KERNEL)) == NULL) {
  178. return NULL;
  179. }
  180. INIT_LIST_HEAD(&t->tc);
  181. list_add_tail(&t->list, &vpecontrol.tc_list);
  182. t->index = index;
  183. return t;
  184. }
  185. /* clean up and free everything */
  186. void release_vpe(struct vpe *v)
  187. {
  188. list_del(&v->list);
  189. if (v->load_addr)
  190. release_progmem(v);
  191. kfree(v);
  192. }
  193. void dump_mtregs(void)
  194. {
  195. unsigned long val;
  196. val = read_c0_config3();
  197. printk("config3 0x%lx MT %ld\n", val,
  198. (val & CONFIG3_MT) >> CONFIG3_MT_SHIFT);
  199. val = read_c0_mvpcontrol();
  200. printk("MVPControl 0x%lx, STLB %ld VPC %ld EVP %ld\n", val,
  201. (val & MVPCONTROL_STLB) >> MVPCONTROL_STLB_SHIFT,
  202. (val & MVPCONTROL_VPC) >> MVPCONTROL_VPC_SHIFT,
  203. (val & MVPCONTROL_EVP));
  204. val = read_c0_mvpconf0();
  205. printk("mvpconf0 0x%lx, PVPE %ld PTC %ld M %ld\n", val,
  206. (val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT,
  207. val & MVPCONF0_PTC, (val & MVPCONF0_M) >> MVPCONF0_M_SHIFT);
  208. }
  209. /* Find some VPE program space */
  210. static void *alloc_progmem(unsigned long len)
  211. {
  212. #ifdef CONFIG_MIPS_VPE_LOADER_TOM
  213. /* this means you must tell linux to use less memory than you physically have */
  214. return pfn_to_kaddr(max_pfn);
  215. #else
  216. // simple grab some mem for now
  217. return kmalloc(len, GFP_KERNEL);
  218. #endif
  219. }
  220. static void release_progmem(void *ptr)
  221. {
  222. #ifndef CONFIG_MIPS_VPE_LOADER_TOM
  223. kfree(ptr);
  224. #endif
  225. }
  226. /* Update size with this section: return offset. */
  227. static long get_offset(unsigned long *size, Elf_Shdr * sechdr)
  228. {
  229. long ret;
  230. ret = ALIGN(*size, sechdr->sh_addralign ? : 1);
  231. *size = ret + sechdr->sh_size;
  232. return ret;
  233. }
  234. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  235. might -- code, read-only data, read-write data, small data. Tally
  236. sizes, and place the offsets into sh_entsize fields: high bit means it
  237. belongs in init. */
  238. static void layout_sections(struct module *mod, const Elf_Ehdr * hdr,
  239. Elf_Shdr * sechdrs, const char *secstrings)
  240. {
  241. static unsigned long const masks[][2] = {
  242. /* NOTE: all executable code must be the first section
  243. * in this array; otherwise modify the text_size
  244. * finder in the two loops below */
  245. {SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL},
  246. {SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL},
  247. {SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL},
  248. {ARCH_SHF_SMALL | SHF_ALLOC, 0}
  249. };
  250. unsigned int m, i;
  251. for (i = 0; i < hdr->e_shnum; i++)
  252. sechdrs[i].sh_entsize = ~0UL;
  253. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  254. for (i = 0; i < hdr->e_shnum; ++i) {
  255. Elf_Shdr *s = &sechdrs[i];
  256. // || strncmp(secstrings + s->sh_name, ".init", 5) == 0)
  257. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  258. || (s->sh_flags & masks[m][1])
  259. || s->sh_entsize != ~0UL)
  260. continue;
  261. s->sh_entsize = get_offset(&mod->core_size, s);
  262. }
  263. if (m == 0)
  264. mod->core_text_size = mod->core_size;
  265. }
  266. }
  267. /* from module-elf32.c, but subverted a little */
  268. struct mips_hi16 {
  269. struct mips_hi16 *next;
  270. Elf32_Addr *addr;
  271. Elf32_Addr value;
  272. };
  273. static struct mips_hi16 *mips_hi16_list;
  274. static unsigned int gp_offs, gp_addr;
  275. static int apply_r_mips_none(struct module *me, uint32_t *location,
  276. Elf32_Addr v)
  277. {
  278. return 0;
  279. }
  280. static int apply_r_mips_gprel16(struct module *me, uint32_t *location,
  281. Elf32_Addr v)
  282. {
  283. int rel;
  284. if( !(*location & 0xffff) ) {
  285. rel = (int)v - gp_addr;
  286. }
  287. else {
  288. /* .sbss + gp(relative) + offset */
  289. /* kludge! */
  290. rel = (int)(short)((int)v + gp_offs +
  291. (int)(short)(*location & 0xffff) - gp_addr);
  292. }
  293. if( (rel > 32768) || (rel < -32768) ) {
  294. printk(KERN_DEBUG "VPE loader: apply_r_mips_gprel16: "
  295. "relative address 0x%x out of range of gp register\n",
  296. rel);
  297. return -ENOEXEC;
  298. }
  299. *location = (*location & 0xffff0000) | (rel & 0xffff);
  300. return 0;
  301. }
  302. static int apply_r_mips_pc16(struct module *me, uint32_t *location,
  303. Elf32_Addr v)
  304. {
  305. int rel;
  306. rel = (((unsigned int)v - (unsigned int)location));
  307. rel >>= 2; // because the offset is in _instructions_ not bytes.
  308. rel -= 1; // and one instruction less due to the branch delay slot.
  309. if( (rel > 32768) || (rel < -32768) ) {
  310. printk(KERN_DEBUG "VPE loader: "
  311. "apply_r_mips_pc16: relative address out of range 0x%x\n", rel);
  312. return -ENOEXEC;
  313. }
  314. *location = (*location & 0xffff0000) | (rel & 0xffff);
  315. return 0;
  316. }
  317. static int apply_r_mips_32(struct module *me, uint32_t *location,
  318. Elf32_Addr v)
  319. {
  320. *location += v;
  321. return 0;
  322. }
  323. static int apply_r_mips_26(struct module *me, uint32_t *location,
  324. Elf32_Addr v)
  325. {
  326. if (v % 4) {
  327. printk(KERN_DEBUG "VPE loader: apply_r_mips_26 "
  328. " unaligned relocation\n");
  329. return -ENOEXEC;
  330. }
  331. /*
  332. * Not desperately convinced this is a good check of an overflow condition
  333. * anyway. But it gets in the way of handling undefined weak symbols which
  334. * we want to set to zero.
  335. * if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  336. * printk(KERN_ERR
  337. * "module %s: relocation overflow\n",
  338. * me->name);
  339. * return -ENOEXEC;
  340. * }
  341. */
  342. *location = (*location & ~0x03ffffff) |
  343. ((*location + (v >> 2)) & 0x03ffffff);
  344. return 0;
  345. }
  346. static int apply_r_mips_hi16(struct module *me, uint32_t *location,
  347. Elf32_Addr v)
  348. {
  349. struct mips_hi16 *n;
  350. /*
  351. * We cannot relocate this one now because we don't know the value of
  352. * the carry we need to add. Save the information, and let LO16 do the
  353. * actual relocation.
  354. */
  355. n = kmalloc(sizeof *n, GFP_KERNEL);
  356. if (!n)
  357. return -ENOMEM;
  358. n->addr = location;
  359. n->value = v;
  360. n->next = mips_hi16_list;
  361. mips_hi16_list = n;
  362. return 0;
  363. }
  364. static int apply_r_mips_lo16(struct module *me, uint32_t *location,
  365. Elf32_Addr v)
  366. {
  367. unsigned long insnlo = *location;
  368. Elf32_Addr val, vallo;
  369. /* Sign extend the addend we extract from the lo insn. */
  370. vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
  371. if (mips_hi16_list != NULL) {
  372. struct mips_hi16 *l;
  373. l = mips_hi16_list;
  374. while (l != NULL) {
  375. struct mips_hi16 *next;
  376. unsigned long insn;
  377. /*
  378. * The value for the HI16 had best be the same.
  379. */
  380. if (v != l->value) {
  381. printk(KERN_DEBUG "VPE loader: "
  382. "apply_r_mips_lo16/hi16: "
  383. "inconsistent value information\n");
  384. return -ENOEXEC;
  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. }
  416. static int (*reloc_handlers[]) (struct module *me, uint32_t *location,
  417. Elf32_Addr v) = {
  418. [R_MIPS_NONE] = apply_r_mips_none,
  419. [R_MIPS_32] = apply_r_mips_32,
  420. [R_MIPS_26] = apply_r_mips_26,
  421. [R_MIPS_HI16] = apply_r_mips_hi16,
  422. [R_MIPS_LO16] = apply_r_mips_lo16,
  423. [R_MIPS_GPREL16] = apply_r_mips_gprel16,
  424. [R_MIPS_PC16] = apply_r_mips_pc16
  425. };
  426. static char *rstrs[] = {
  427. [R_MIPS_NONE] = "MIPS_NONE",
  428. [R_MIPS_32] = "MIPS_32",
  429. [R_MIPS_26] = "MIPS_26",
  430. [R_MIPS_HI16] = "MIPS_HI16",
  431. [R_MIPS_LO16] = "MIPS_LO16",
  432. [R_MIPS_GPREL16] = "MIPS_GPREL16",
  433. [R_MIPS_PC16] = "MIPS_PC16"
  434. };
  435. int apply_relocations(Elf32_Shdr *sechdrs,
  436. const char *strtab,
  437. unsigned int symindex,
  438. unsigned int relsec,
  439. struct module *me)
  440. {
  441. Elf32_Rel *rel = (void *) sechdrs[relsec].sh_addr;
  442. Elf32_Sym *sym;
  443. uint32_t *location;
  444. unsigned int i;
  445. Elf32_Addr v;
  446. int res;
  447. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  448. Elf32_Word r_info = rel[i].r_info;
  449. /* This is where to make the change */
  450. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  451. + rel[i].r_offset;
  452. /* This is the symbol it is referring to */
  453. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  454. + ELF32_R_SYM(r_info);
  455. if (!sym->st_value) {
  456. printk(KERN_DEBUG "%s: undefined weak symbol %s\n",
  457. me->name, strtab + sym->st_name);
  458. /* just print the warning, dont barf */
  459. }
  460. v = sym->st_value;
  461. res = reloc_handlers[ELF32_R_TYPE(r_info)](me, location, v);
  462. if( res ) {
  463. char *r = rstrs[ELF32_R_TYPE(r_info)];
  464. printk(KERN_WARNING "VPE loader: .text+0x%x "
  465. "relocation type %s for symbol \"%s\" failed\n",
  466. rel[i].r_offset, r ? r : "UNKNOWN",
  467. strtab + sym->st_name);
  468. return res;
  469. }
  470. }
  471. return 0;
  472. }
  473. void save_gp_address(unsigned int secbase, unsigned int rel)
  474. {
  475. gp_addr = secbase + rel;
  476. gp_offs = gp_addr - (secbase & 0xffff0000);
  477. }
  478. /* end module-elf32.c */
  479. /* Change all symbols so that sh_value encodes the pointer directly. */
  480. static void simplify_symbols(Elf_Shdr * sechdrs,
  481. unsigned int symindex,
  482. const char *strtab,
  483. const char *secstrings,
  484. unsigned int nsecs, struct module *mod)
  485. {
  486. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  487. unsigned long secbase, bssbase = 0;
  488. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  489. int size;
  490. /* find the .bss section for COMMON symbols */
  491. for (i = 0; i < nsecs; i++) {
  492. if (strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) == 0) {
  493. bssbase = sechdrs[i].sh_addr;
  494. break;
  495. }
  496. }
  497. for (i = 1; i < n; i++) {
  498. switch (sym[i].st_shndx) {
  499. case SHN_COMMON:
  500. /* Allocate space for the symbol in the .bss section.
  501. st_value is currently size.
  502. We want it to have the address of the symbol. */
  503. size = sym[i].st_value;
  504. sym[i].st_value = bssbase;
  505. bssbase += size;
  506. break;
  507. case SHN_ABS:
  508. /* Don't need to do anything */
  509. break;
  510. case SHN_UNDEF:
  511. /* ret = -ENOENT; */
  512. break;
  513. case SHN_MIPS_SCOMMON:
  514. printk(KERN_DEBUG "simplify_symbols: ignoring SHN_MIPS_SCOMMON"
  515. "symbol <%s> st_shndx %d\n", strtab + sym[i].st_name,
  516. sym[i].st_shndx);
  517. // .sbss section
  518. break;
  519. default:
  520. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  521. if (strncmp(strtab + sym[i].st_name, "_gp", 3) == 0) {
  522. save_gp_address(secbase, sym[i].st_value);
  523. }
  524. sym[i].st_value += secbase;
  525. break;
  526. }
  527. }
  528. }
  529. #ifdef DEBUG_ELFLOADER
  530. static void dump_elfsymbols(Elf_Shdr * sechdrs, unsigned int symindex,
  531. const char *strtab, struct module *mod)
  532. {
  533. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  534. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  535. printk(KERN_DEBUG "dump_elfsymbols: n %d\n", n);
  536. for (i = 1; i < n; i++) {
  537. printk(KERN_DEBUG " i %d name <%s> 0x%x\n", i,
  538. strtab + sym[i].st_name, sym[i].st_value);
  539. }
  540. }
  541. #endif
  542. static void dump_tc(struct tc *t)
  543. {
  544. unsigned long val;
  545. settc(t->index);
  546. printk(KERN_DEBUG "VPE loader: TC index %d targtc %ld "
  547. "TCStatus 0x%lx halt 0x%lx\n",
  548. t->index, read_c0_vpecontrol() & VPECONTROL_TARGTC,
  549. read_tc_c0_tcstatus(), read_tc_c0_tchalt());
  550. printk(KERN_DEBUG " tcrestart 0x%lx\n", read_tc_c0_tcrestart());
  551. printk(KERN_DEBUG " tcbind 0x%lx\n", read_tc_c0_tcbind());
  552. val = read_c0_vpeconf0();
  553. printk(KERN_DEBUG " VPEConf0 0x%lx MVP %ld\n", val,
  554. (val & VPECONF0_MVP) >> VPECONF0_MVP_SHIFT);
  555. printk(KERN_DEBUG " c0 status 0x%lx\n", read_vpe_c0_status());
  556. printk(KERN_DEBUG " c0 cause 0x%lx\n", read_vpe_c0_cause());
  557. printk(KERN_DEBUG " c0 badvaddr 0x%lx\n", read_vpe_c0_badvaddr());
  558. printk(KERN_DEBUG " c0 epc 0x%lx\n", read_vpe_c0_epc());
  559. }
  560. static void dump_tclist(void)
  561. {
  562. struct tc *t;
  563. list_for_each_entry(t, &vpecontrol.tc_list, list) {
  564. dump_tc(t);
  565. }
  566. }
  567. /* We are prepared so configure and start the VPE... */
  568. static int vpe_run(struct vpe * v)
  569. {
  570. struct vpe_notifications *n;
  571. unsigned long val, dmt_flag;
  572. struct tc *t;
  573. /* check we are the Master VPE */
  574. val = read_c0_vpeconf0();
  575. if (!(val & VPECONF0_MVP)) {
  576. printk(KERN_WARNING
  577. "VPE loader: only Master VPE's are allowed to configure MT\n");
  578. return -1;
  579. }
  580. /* disable MT (using dvpe) */
  581. dvpe();
  582. if (!list_empty(&v->tc)) {
  583. if ((t = list_entry(v->tc.next, struct tc, tc)) == NULL) {
  584. printk(KERN_WARNING "VPE loader: TC %d is already in use.\n",
  585. t->index);
  586. return -ENOEXEC;
  587. }
  588. } else {
  589. printk(KERN_WARNING "VPE loader: No TC's associated with VPE %d\n",
  590. v->minor);
  591. return -ENOEXEC;
  592. }
  593. /* Put MVPE's into 'configuration state' */
  594. set_c0_mvpcontrol(MVPCONTROL_VPC);
  595. settc(t->index);
  596. /* should check it is halted, and not activated */
  597. if ((read_tc_c0_tcstatus() & TCSTATUS_A) || !(read_tc_c0_tchalt() & TCHALT_H)) {
  598. printk(KERN_WARNING "VPE loader: TC %d is already doing something!\n",
  599. t->index);
  600. dump_tclist();
  601. return -ENOEXEC;
  602. }
  603. /*
  604. * Disable multi-threaded execution whilst we activate, clear the
  605. * halt bit and bound the tc to the other VPE...
  606. */
  607. dmt_flag = dmt();
  608. /* Write the address we want it to start running from in the TCPC register. */
  609. write_tc_c0_tcrestart((unsigned long)v->__start);
  610. write_tc_c0_tccontext((unsigned long)0);
  611. /*
  612. * Mark the TC as activated, not interrupt exempt and not dynamically
  613. * allocatable
  614. */
  615. val = read_tc_c0_tcstatus();
  616. val = (val & ~(TCSTATUS_DA | TCSTATUS_IXMT)) | TCSTATUS_A;
  617. write_tc_c0_tcstatus(val);
  618. write_tc_c0_tchalt(read_tc_c0_tchalt() & ~TCHALT_H);
  619. /*
  620. * The sde-kit passes 'memsize' to __start in $a3, so set something
  621. * here... Or set $a3 to zero and define DFLT_STACK_SIZE and
  622. * DFLT_HEAP_SIZE when you compile your program
  623. */
  624. mttgpr(7, physical_memsize);
  625. /* set up VPE1 */
  626. /*
  627. * bind the TC to VPE 1 as late as possible so we only have the final
  628. * VPE registers to set up, and so an EJTAG probe can trigger on it
  629. */
  630. write_tc_c0_tcbind((read_tc_c0_tcbind() & ~TCBIND_CURVPE) | v->minor);
  631. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~(VPECONF0_VPA));
  632. back_to_back_c0_hazard();
  633. /* Set up the XTC bit in vpeconf0 to point at our tc */
  634. write_vpe_c0_vpeconf0( (read_vpe_c0_vpeconf0() & ~(VPECONF0_XTC))
  635. | (t->index << VPECONF0_XTC_SHIFT));
  636. back_to_back_c0_hazard();
  637. /* enable this VPE */
  638. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA);
  639. /* clear out any left overs from a previous program */
  640. write_vpe_c0_status(0);
  641. write_vpe_c0_cause(0);
  642. /* take system out of configuration state */
  643. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  644. /* now safe to re-enable multi-threading */
  645. emt(dmt_flag);
  646. /* set it running */
  647. evpe(EVPE_ENABLE);
  648. list_for_each_entry(n, &v->notify, list) {
  649. n->start(v->minor);
  650. }
  651. return 0;
  652. }
  653. static int find_vpe_symbols(struct vpe * v, Elf_Shdr * sechdrs,
  654. unsigned int symindex, const char *strtab,
  655. struct module *mod)
  656. {
  657. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  658. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  659. for (i = 1; i < n; i++) {
  660. if (strcmp(strtab + sym[i].st_name, "__start") == 0) {
  661. v->__start = sym[i].st_value;
  662. }
  663. if (strcmp(strtab + sym[i].st_name, "vpe_shared") == 0) {
  664. v->shared_ptr = (void *)sym[i].st_value;
  665. }
  666. }
  667. if ( (v->__start == 0) || (v->shared_ptr == NULL))
  668. return -1;
  669. return 0;
  670. }
  671. /*
  672. * Allocates a VPE with some program code space(the load address), copies the
  673. * contents of the program (p)buffer performing relocatations/etc, free's it
  674. * when finished.
  675. */
  676. static int vpe_elfload(struct vpe * v)
  677. {
  678. Elf_Ehdr *hdr;
  679. Elf_Shdr *sechdrs;
  680. long err = 0;
  681. char *secstrings, *strtab = NULL;
  682. unsigned int len, i, symindex = 0, strindex = 0, relocate = 0;
  683. struct module mod; // so we can re-use the relocations code
  684. memset(&mod, 0, sizeof(struct module));
  685. strcpy(mod.name, "VPE loader");
  686. hdr = (Elf_Ehdr *) v->pbuffer;
  687. len = v->plen;
  688. /* Sanity checks against insmoding binaries or wrong arch,
  689. weird elf version */
  690. if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
  691. || (hdr->e_type != ET_REL && hdr->e_type != ET_EXEC)
  692. || !elf_check_arch(hdr)
  693. || hdr->e_shentsize != sizeof(*sechdrs)) {
  694. printk(KERN_WARNING
  695. "VPE loader: program wrong arch or weird elf version\n");
  696. return -ENOEXEC;
  697. }
  698. if (hdr->e_type == ET_REL)
  699. relocate = 1;
  700. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
  701. printk(KERN_ERR "VPE loader: program length %u truncated\n",
  702. len);
  703. return -ENOEXEC;
  704. }
  705. /* Convenience variables */
  706. sechdrs = (void *)hdr + hdr->e_shoff;
  707. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  708. sechdrs[0].sh_addr = 0;
  709. /* And these should exist, but gcc whinges if we don't init them */
  710. symindex = strindex = 0;
  711. if (relocate) {
  712. for (i = 1; i < hdr->e_shnum; i++) {
  713. if (sechdrs[i].sh_type != SHT_NOBITS
  714. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size) {
  715. printk(KERN_ERR "VPE program length %u truncated\n",
  716. len);
  717. return -ENOEXEC;
  718. }
  719. /* Mark all sections sh_addr with their address in the
  720. temporary image. */
  721. sechdrs[i].sh_addr = (size_t) hdr + sechdrs[i].sh_offset;
  722. /* Internal symbols and strings. */
  723. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  724. symindex = i;
  725. strindex = sechdrs[i].sh_link;
  726. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  727. }
  728. }
  729. layout_sections(&mod, hdr, sechdrs, secstrings);
  730. }
  731. v->load_addr = alloc_progmem(mod.core_size);
  732. memset(v->load_addr, 0, mod.core_size);
  733. printk("VPE loader: loading to %p\n", v->load_addr);
  734. if (relocate) {
  735. for (i = 0; i < hdr->e_shnum; i++) {
  736. void *dest;
  737. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  738. continue;
  739. dest = v->load_addr + sechdrs[i].sh_entsize;
  740. if (sechdrs[i].sh_type != SHT_NOBITS)
  741. memcpy(dest, (void *)sechdrs[i].sh_addr,
  742. sechdrs[i].sh_size);
  743. /* Update sh_addr to point to copy in image. */
  744. sechdrs[i].sh_addr = (unsigned long)dest;
  745. printk(KERN_DEBUG " section sh_name %s sh_addr 0x%x\n",
  746. secstrings + sechdrs[i].sh_name, sechdrs[i].sh_addr);
  747. }
  748. /* Fix up syms, so that st_value is a pointer to location. */
  749. simplify_symbols(sechdrs, symindex, strtab, secstrings,
  750. hdr->e_shnum, &mod);
  751. /* Now do relocations. */
  752. for (i = 1; i < hdr->e_shnum; i++) {
  753. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  754. unsigned int info = sechdrs[i].sh_info;
  755. /* Not a valid relocation section? */
  756. if (info >= hdr->e_shnum)
  757. continue;
  758. /* Don't bother with non-allocated sections */
  759. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  760. continue;
  761. if (sechdrs[i].sh_type == SHT_REL)
  762. err = apply_relocations(sechdrs, strtab, symindex, i,
  763. &mod);
  764. else if (sechdrs[i].sh_type == SHT_RELA)
  765. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  766. &mod);
  767. if (err < 0)
  768. return err;
  769. }
  770. } else {
  771. for (i = 0; i < hdr->e_shnum; i++) {
  772. /* Internal symbols and strings. */
  773. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  774. symindex = i;
  775. strindex = sechdrs[i].sh_link;
  776. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  777. /* mark the symtab's address for when we try to find the
  778. magic symbols */
  779. sechdrs[i].sh_addr = (size_t) hdr + sechdrs[i].sh_offset;
  780. }
  781. /* filter sections we dont want in the final image */
  782. if (!(sechdrs[i].sh_flags & SHF_ALLOC) ||
  783. (sechdrs[i].sh_type == SHT_MIPS_REGINFO)) {
  784. printk( KERN_DEBUG " ignoring section, "
  785. "name %s type %x address 0x%x \n",
  786. secstrings + sechdrs[i].sh_name,
  787. sechdrs[i].sh_type, sechdrs[i].sh_addr);
  788. continue;
  789. }
  790. if (sechdrs[i].sh_addr < (unsigned int)v->load_addr) {
  791. printk( KERN_WARNING "VPE loader: "
  792. "fully linked image has invalid section, "
  793. "name %s type %x address 0x%x, before load "
  794. "address of 0x%x\n",
  795. secstrings + sechdrs[i].sh_name,
  796. sechdrs[i].sh_type, sechdrs[i].sh_addr,
  797. (unsigned int)v->load_addr);
  798. return -ENOEXEC;
  799. }
  800. printk(KERN_DEBUG " copying section sh_name %s, sh_addr 0x%x "
  801. "size 0x%x0 from x%p\n",
  802. secstrings + sechdrs[i].sh_name, sechdrs[i].sh_addr,
  803. sechdrs[i].sh_size, hdr + sechdrs[i].sh_offset);
  804. if (sechdrs[i].sh_type != SHT_NOBITS)
  805. memcpy((void *)sechdrs[i].sh_addr,
  806. (char *)hdr + sechdrs[i].sh_offset,
  807. sechdrs[i].sh_size);
  808. else
  809. memset((void *)sechdrs[i].sh_addr, 0, sechdrs[i].sh_size);
  810. }
  811. }
  812. /* make sure it's physically written out */
  813. flush_icache_range((unsigned long)v->load_addr,
  814. (unsigned long)v->load_addr + v->len);
  815. if ((find_vpe_symbols(v, sechdrs, symindex, strtab, &mod)) < 0) {
  816. if (v->__start == 0) {
  817. printk(KERN_WARNING "VPE loader: program does not contain "
  818. "a __start symbol\n");
  819. return -ENOEXEC;
  820. }
  821. if (v->shared_ptr == NULL)
  822. printk(KERN_WARNING "VPE loader: "
  823. "program does not contain vpe_shared symbol.\n"
  824. " Unable to use AMVP (AP/SP) facilities.\n");
  825. }
  826. printk(" elf loaded\n");
  827. return 0;
  828. }
  829. __attribute_used__ void dump_vpe(struct vpe * v)
  830. {
  831. struct tc *t;
  832. settc(v->minor);
  833. printk(KERN_DEBUG "VPEControl 0x%lx\n", read_vpe_c0_vpecontrol());
  834. printk(KERN_DEBUG "VPEConf0 0x%lx\n", read_vpe_c0_vpeconf0());
  835. list_for_each_entry(t, &vpecontrol.tc_list, list)
  836. dump_tc(t);
  837. }
  838. static void cleanup_tc(struct tc *tc)
  839. {
  840. int tmp;
  841. /* Put MVPE's into 'configuration state' */
  842. set_c0_mvpcontrol(MVPCONTROL_VPC);
  843. settc(tc->index);
  844. tmp = read_tc_c0_tcstatus();
  845. /* mark not allocated and not dynamically allocatable */
  846. tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
  847. tmp |= TCSTATUS_IXMT; /* interrupt exempt */
  848. write_tc_c0_tcstatus(tmp);
  849. write_tc_c0_tchalt(TCHALT_H);
  850. /* bind it to anything other than VPE1 */
  851. write_tc_c0_tcbind(read_tc_c0_tcbind() & ~TCBIND_CURVPE); // | TCBIND_CURVPE
  852. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  853. }
  854. static int getcwd(char *buff, int size)
  855. {
  856. mm_segment_t old_fs;
  857. int ret;
  858. old_fs = get_fs();
  859. set_fs(KERNEL_DS);
  860. ret = sys_getcwd(buff,size);
  861. set_fs(old_fs);
  862. return ret;
  863. }
  864. /* checks VPE is unused and gets ready to load program */
  865. static int vpe_open(struct inode *inode, struct file *filp)
  866. {
  867. int minor, ret;
  868. struct vpe *v;
  869. struct vpe_notifications *not;
  870. /* assume only 1 device at the mo. */
  871. if ((minor = iminor(inode)) != 1) {
  872. printk(KERN_WARNING "VPE loader: only vpe1 is supported\n");
  873. return -ENODEV;
  874. }
  875. if ((v = get_vpe(minor)) == NULL) {
  876. printk(KERN_WARNING "VPE loader: unable to get vpe\n");
  877. return -ENODEV;
  878. }
  879. if (v->state != VPE_STATE_UNUSED) {
  880. dvpe();
  881. printk(KERN_DEBUG "VPE loader: tc in use dumping regs\n");
  882. dump_tc(get_tc(minor));
  883. list_for_each_entry(not, &v->notify, list) {
  884. not->stop(minor);
  885. }
  886. release_progmem(v->load_addr);
  887. cleanup_tc(get_tc(minor));
  888. }
  889. // allocate it so when we get write ops we know it's expected.
  890. v->state = VPE_STATE_INUSE;
  891. /* this of-course trashes what was there before... */
  892. v->pbuffer = vmalloc(P_SIZE);
  893. v->plen = P_SIZE;
  894. v->load_addr = NULL;
  895. v->len = 0;
  896. v->uid = filp->f_uid;
  897. v->gid = filp->f_gid;
  898. #ifdef CONFIG_MIPS_APSP_KSPD
  899. /* get kspd to tell us when a syscall_exit happens */
  900. if (!kspd_events_reqd) {
  901. kspd_notify(&kspd_events);
  902. kspd_events_reqd++;
  903. }
  904. #endif
  905. v->cwd[0] = 0;
  906. ret = getcwd(v->cwd, VPE_PATH_MAX);
  907. if (ret < 0)
  908. printk(KERN_WARNING "VPE loader: open, getcwd returned %d\n", ret);
  909. v->shared_ptr = NULL;
  910. v->__start = 0;
  911. return 0;
  912. }
  913. static int vpe_release(struct inode *inode, struct file *filp)
  914. {
  915. int minor, ret = 0;
  916. struct vpe *v;
  917. Elf_Ehdr *hdr;
  918. minor = iminor(inode);
  919. if ((v = get_vpe(minor)) == NULL)
  920. return -ENODEV;
  921. // simple case of fire and forget, so tell the VPE to run...
  922. hdr = (Elf_Ehdr *) v->pbuffer;
  923. if (memcmp(hdr->e_ident, ELFMAG, 4) == 0) {
  924. if (vpe_elfload(v) >= 0)
  925. vpe_run(v);
  926. else {
  927. printk(KERN_WARNING "VPE loader: ELF load failed.\n");
  928. ret = -ENOEXEC;
  929. }
  930. } else {
  931. printk(KERN_WARNING "VPE loader: only elf files are supported\n");
  932. ret = -ENOEXEC;
  933. }
  934. /* It's good to be able to run the SP and if it chokes have a look at
  935. the /dev/rt?. But if we reset the pointer to the shared struct we
  936. loose what has happened. So perhaps if garbage is sent to the vpe
  937. device, use it as a trigger for the reset. Hopefully a nice
  938. executable will be along shortly. */
  939. if (ret < 0)
  940. v->shared_ptr = NULL;
  941. // cleanup any temp buffers
  942. if (v->pbuffer)
  943. vfree(v->pbuffer);
  944. v->plen = 0;
  945. return ret;
  946. }
  947. static ssize_t vpe_write(struct file *file, const char __user * buffer,
  948. size_t count, loff_t * ppos)
  949. {
  950. int minor;
  951. size_t ret = count;
  952. struct vpe *v;
  953. minor = iminor(file->f_path.dentry->d_inode);
  954. if ((v = get_vpe(minor)) == NULL)
  955. return -ENODEV;
  956. if (v->pbuffer == NULL) {
  957. printk(KERN_ERR "VPE loader: no buffer for program\n");
  958. return -ENOMEM;
  959. }
  960. if ((count + v->len) > v->plen) {
  961. printk(KERN_WARNING
  962. "VPE loader: elf size too big. Perhaps strip uneeded symbols\n");
  963. return -ENOMEM;
  964. }
  965. count -= copy_from_user(v->pbuffer + v->len, buffer, count);
  966. if (!count)
  967. return -EFAULT;
  968. v->len += count;
  969. return ret;
  970. }
  971. static struct file_operations vpe_fops = {
  972. .owner = THIS_MODULE,
  973. .open = vpe_open,
  974. .release = vpe_release,
  975. .write = vpe_write
  976. };
  977. /* module wrapper entry points */
  978. /* give me a vpe */
  979. vpe_handle vpe_alloc(void)
  980. {
  981. int i;
  982. struct vpe *v;
  983. /* find a vpe */
  984. for (i = 1; i < MAX_VPES; i++) {
  985. if ((v = get_vpe(i)) != NULL) {
  986. v->state = VPE_STATE_INUSE;
  987. return v;
  988. }
  989. }
  990. return NULL;
  991. }
  992. EXPORT_SYMBOL(vpe_alloc);
  993. /* start running from here */
  994. int vpe_start(vpe_handle vpe, unsigned long start)
  995. {
  996. struct vpe *v = vpe;
  997. v->__start = start;
  998. return vpe_run(v);
  999. }
  1000. EXPORT_SYMBOL(vpe_start);
  1001. /* halt it for now */
  1002. int vpe_stop(vpe_handle vpe)
  1003. {
  1004. struct vpe *v = vpe;
  1005. struct tc *t;
  1006. unsigned int evpe_flags;
  1007. evpe_flags = dvpe();
  1008. if ((t = list_entry(v->tc.next, struct tc, tc)) != NULL) {
  1009. settc(t->index);
  1010. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~VPECONF0_VPA);
  1011. }
  1012. evpe(evpe_flags);
  1013. return 0;
  1014. }
  1015. EXPORT_SYMBOL(vpe_stop);
  1016. /* I've done with it thank you */
  1017. int vpe_free(vpe_handle vpe)
  1018. {
  1019. struct vpe *v = vpe;
  1020. struct tc *t;
  1021. unsigned int evpe_flags;
  1022. if ((t = list_entry(v->tc.next, struct tc, tc)) == NULL) {
  1023. return -ENOEXEC;
  1024. }
  1025. evpe_flags = dvpe();
  1026. /* Put MVPE's into 'configuration state' */
  1027. set_c0_mvpcontrol(MVPCONTROL_VPC);
  1028. settc(t->index);
  1029. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~VPECONF0_VPA);
  1030. /* mark the TC unallocated and halt'ed */
  1031. write_tc_c0_tcstatus(read_tc_c0_tcstatus() & ~TCSTATUS_A);
  1032. write_tc_c0_tchalt(TCHALT_H);
  1033. v->state = VPE_STATE_UNUSED;
  1034. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  1035. evpe(evpe_flags);
  1036. return 0;
  1037. }
  1038. EXPORT_SYMBOL(vpe_free);
  1039. void *vpe_get_shared(int index)
  1040. {
  1041. struct vpe *v;
  1042. if ((v = get_vpe(index)) == NULL)
  1043. return NULL;
  1044. return v->shared_ptr;
  1045. }
  1046. EXPORT_SYMBOL(vpe_get_shared);
  1047. int vpe_getuid(int index)
  1048. {
  1049. struct vpe *v;
  1050. if ((v = get_vpe(index)) == NULL)
  1051. return -1;
  1052. return v->uid;
  1053. }
  1054. EXPORT_SYMBOL(vpe_getuid);
  1055. int vpe_getgid(int index)
  1056. {
  1057. struct vpe *v;
  1058. if ((v = get_vpe(index)) == NULL)
  1059. return -1;
  1060. return v->gid;
  1061. }
  1062. EXPORT_SYMBOL(vpe_getgid);
  1063. int vpe_notify(int index, struct vpe_notifications *notify)
  1064. {
  1065. struct vpe *v;
  1066. if ((v = get_vpe(index)) == NULL)
  1067. return -1;
  1068. list_add(&notify->list, &v->notify);
  1069. return 0;
  1070. }
  1071. EXPORT_SYMBOL(vpe_notify);
  1072. char *vpe_getcwd(int index)
  1073. {
  1074. struct vpe *v;
  1075. if ((v = get_vpe(index)) == NULL)
  1076. return NULL;
  1077. return v->cwd;
  1078. }
  1079. EXPORT_SYMBOL(vpe_getcwd);
  1080. #ifdef CONFIG_MIPS_APSP_KSPD
  1081. static void kspd_sp_exit( int sp_id)
  1082. {
  1083. cleanup_tc(get_tc(sp_id));
  1084. }
  1085. #endif
  1086. static int __init vpe_module_init(void)
  1087. {
  1088. struct vpe *v = NULL;
  1089. struct tc *t;
  1090. unsigned long val;
  1091. int i;
  1092. if (!cpu_has_mipsmt) {
  1093. printk("VPE loader: not a MIPS MT capable processor\n");
  1094. return -ENODEV;
  1095. }
  1096. major = register_chrdev(0, module_name, &vpe_fops);
  1097. if (major < 0) {
  1098. printk("VPE loader: unable to register character device\n");
  1099. return major;
  1100. }
  1101. dmt();
  1102. dvpe();
  1103. /* Put MVPE's into 'configuration state' */
  1104. set_c0_mvpcontrol(MVPCONTROL_VPC);
  1105. /* dump_mtregs(); */
  1106. val = read_c0_mvpconf0();
  1107. for (i = 0; i < ((val & MVPCONF0_PTC) + 1); i++) {
  1108. t = alloc_tc(i);
  1109. /* VPE's */
  1110. if (i < ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1) {
  1111. settc(i);
  1112. if ((v = alloc_vpe(i)) == NULL) {
  1113. printk(KERN_WARNING "VPE: unable to allocate VPE\n");
  1114. return -ENODEV;
  1115. }
  1116. /* add the tc to the list of this vpe's tc's. */
  1117. list_add(&t->tc, &v->tc);
  1118. /* deactivate all but vpe0 */
  1119. if (i != 0) {
  1120. unsigned long tmp = read_vpe_c0_vpeconf0();
  1121. tmp &= ~VPECONF0_VPA;
  1122. /* master VPE */
  1123. tmp |= VPECONF0_MVP;
  1124. write_vpe_c0_vpeconf0(tmp);
  1125. }
  1126. /* disable multi-threading with TC's */
  1127. write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() & ~VPECONTROL_TE);
  1128. if (i != 0) {
  1129. write_vpe_c0_status((read_c0_status() &
  1130. ~(ST0_IM | ST0_IE | ST0_KSU))
  1131. | ST0_CU0);
  1132. /*
  1133. * Set config to be the same as vpe0,
  1134. * particularly kseg0 coherency alg
  1135. */
  1136. write_vpe_c0_config(read_c0_config());
  1137. }
  1138. }
  1139. /* TC's */
  1140. t->pvpe = v; /* set the parent vpe */
  1141. if (i != 0) {
  1142. unsigned long tmp;
  1143. settc(i);
  1144. /* Any TC that is bound to VPE0 gets left as is - in case
  1145. we are running SMTC on VPE0. A TC that is bound to any
  1146. other VPE gets bound to VPE0, ideally I'd like to make
  1147. it homeless but it doesn't appear to let me bind a TC
  1148. to a non-existent VPE. Which is perfectly reasonable.
  1149. The (un)bound state is visible to an EJTAG probe so may
  1150. notify GDB...
  1151. */
  1152. if (((tmp = read_tc_c0_tcbind()) & TCBIND_CURVPE)) {
  1153. /* tc is bound >vpe0 */
  1154. write_tc_c0_tcbind(tmp & ~TCBIND_CURVPE);
  1155. t->pvpe = get_vpe(0); /* set the parent vpe */
  1156. }
  1157. tmp = read_tc_c0_tcstatus();
  1158. /* mark not activated and not dynamically allocatable */
  1159. tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
  1160. tmp |= TCSTATUS_IXMT; /* interrupt exempt */
  1161. write_tc_c0_tcstatus(tmp);
  1162. write_tc_c0_tchalt(TCHALT_H);
  1163. }
  1164. }
  1165. /* release config state */
  1166. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  1167. #ifdef CONFIG_MIPS_APSP_KSPD
  1168. kspd_events.kspd_sp_exit = kspd_sp_exit;
  1169. #endif
  1170. return 0;
  1171. }
  1172. static void __exit vpe_module_exit(void)
  1173. {
  1174. struct vpe *v, *n;
  1175. list_for_each_entry_safe(v, n, &vpecontrol.vpe_list, list) {
  1176. if (v->state != VPE_STATE_UNUSED) {
  1177. release_vpe(v);
  1178. }
  1179. }
  1180. unregister_chrdev(major, module_name);
  1181. }
  1182. module_init(vpe_module_init);
  1183. module_exit(vpe_module_exit);
  1184. MODULE_DESCRIPTION("MIPS VPE Loader");
  1185. MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
  1186. MODULE_LICENSE("GPL");