vpe.c 35 KB

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