vpe.c 35 KB

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