vpe.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~(VPECONF0_VPA));
  629. back_to_back_c0_hazard();
  630. /* Set up the XTC bit in vpeconf0 to point at our tc */
  631. write_vpe_c0_vpeconf0( (read_vpe_c0_vpeconf0() & ~(VPECONF0_XTC))
  632. | (t->index << VPECONF0_XTC_SHIFT));
  633. back_to_back_c0_hazard();
  634. /* enable this VPE */
  635. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA);
  636. /* clear out any left overs from a previous program */
  637. write_vpe_c0_status(0);
  638. write_vpe_c0_cause(0);
  639. /* take system out of configuration state */
  640. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  641. /* now safe to re-enable multi-threading */
  642. emt(dmt_flag);
  643. /* set it running */
  644. evpe(EVPE_ENABLE);
  645. list_for_each_entry(n, &v->notify, list) {
  646. n->start(v->minor);
  647. }
  648. return 0;
  649. }
  650. static int find_vpe_symbols(struct vpe * v, Elf_Shdr * sechdrs,
  651. unsigned int symindex, const char *strtab,
  652. struct module *mod)
  653. {
  654. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  655. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  656. for (i = 1; i < n; i++) {
  657. if (strcmp(strtab + sym[i].st_name, "__start") == 0) {
  658. v->__start = sym[i].st_value;
  659. }
  660. if (strcmp(strtab + sym[i].st_name, "vpe_shared") == 0) {
  661. v->shared_ptr = (void *)sym[i].st_value;
  662. }
  663. }
  664. if ( (v->__start == 0) || (v->shared_ptr == NULL))
  665. return -1;
  666. return 0;
  667. }
  668. /*
  669. * Allocates a VPE with some program code space(the load address), copies the
  670. * contents of the program (p)buffer performing relocatations/etc, free's it
  671. * when finished.
  672. */
  673. int vpe_elfload(struct vpe * v)
  674. {
  675. Elf_Ehdr *hdr;
  676. Elf_Shdr *sechdrs;
  677. long err = 0;
  678. char *secstrings, *strtab = NULL;
  679. unsigned int len, i, symindex = 0, strindex = 0, relocate = 0;
  680. struct module mod; // so we can re-use the relocations code
  681. memset(&mod, 0, sizeof(struct module));
  682. strcpy(mod.name, "VPE loader");
  683. hdr = (Elf_Ehdr *) v->pbuffer;
  684. len = v->plen;
  685. /* Sanity checks against insmoding binaries or wrong arch,
  686. weird elf version */
  687. if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
  688. || (hdr->e_type != ET_REL && hdr->e_type != ET_EXEC)
  689. || !elf_check_arch(hdr)
  690. || hdr->e_shentsize != sizeof(*sechdrs)) {
  691. printk(KERN_WARNING
  692. "VPE loader: program wrong arch or weird elf version\n");
  693. return -ENOEXEC;
  694. }
  695. if (hdr->e_type == ET_REL)
  696. relocate = 1;
  697. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
  698. printk(KERN_ERR "VPE loader: program length %u truncated\n",
  699. len);
  700. return -ENOEXEC;
  701. }
  702. /* Convenience variables */
  703. sechdrs = (void *)hdr + hdr->e_shoff;
  704. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  705. sechdrs[0].sh_addr = 0;
  706. /* And these should exist, but gcc whinges if we don't init them */
  707. symindex = strindex = 0;
  708. if (relocate) {
  709. for (i = 1; i < hdr->e_shnum; i++) {
  710. if (sechdrs[i].sh_type != SHT_NOBITS
  711. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size) {
  712. printk(KERN_ERR "VPE program length %u truncated\n",
  713. len);
  714. return -ENOEXEC;
  715. }
  716. /* Mark all sections sh_addr with their address in the
  717. temporary image. */
  718. sechdrs[i].sh_addr = (size_t) hdr + sechdrs[i].sh_offset;
  719. /* Internal symbols and strings. */
  720. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  721. symindex = i;
  722. strindex = sechdrs[i].sh_link;
  723. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  724. }
  725. }
  726. layout_sections(&mod, hdr, sechdrs, secstrings);
  727. }
  728. v->load_addr = alloc_progmem(mod.core_size);
  729. memset(v->load_addr, 0, mod.core_size);
  730. printk("VPE loader: loading to %p\n", v->load_addr);
  731. if (relocate) {
  732. for (i = 0; i < hdr->e_shnum; i++) {
  733. void *dest;
  734. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  735. continue;
  736. dest = v->load_addr + sechdrs[i].sh_entsize;
  737. if (sechdrs[i].sh_type != SHT_NOBITS)
  738. memcpy(dest, (void *)sechdrs[i].sh_addr,
  739. sechdrs[i].sh_size);
  740. /* Update sh_addr to point to copy in image. */
  741. sechdrs[i].sh_addr = (unsigned long)dest;
  742. printk(KERN_DEBUG " section sh_name %s sh_addr 0x%x\n",
  743. secstrings + sechdrs[i].sh_name, sechdrs[i].sh_addr);
  744. }
  745. /* Fix up syms, so that st_value is a pointer to location. */
  746. simplify_symbols(sechdrs, symindex, strtab, secstrings,
  747. hdr->e_shnum, &mod);
  748. /* Now do relocations. */
  749. for (i = 1; i < hdr->e_shnum; i++) {
  750. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  751. unsigned int info = sechdrs[i].sh_info;
  752. /* Not a valid relocation section? */
  753. if (info >= hdr->e_shnum)
  754. continue;
  755. /* Don't bother with non-allocated sections */
  756. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  757. continue;
  758. if (sechdrs[i].sh_type == SHT_REL)
  759. err = apply_relocations(sechdrs, strtab, symindex, i,
  760. &mod);
  761. else if (sechdrs[i].sh_type == SHT_RELA)
  762. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  763. &mod);
  764. if (err < 0)
  765. return err;
  766. }
  767. } else {
  768. for (i = 0; i < hdr->e_shnum; i++) {
  769. /* Internal symbols and strings. */
  770. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  771. symindex = i;
  772. strindex = sechdrs[i].sh_link;
  773. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  774. /* mark the symtab's address for when we try to find the
  775. magic symbols */
  776. sechdrs[i].sh_addr = (size_t) hdr + sechdrs[i].sh_offset;
  777. }
  778. /* filter sections we dont want in the final image */
  779. if (!(sechdrs[i].sh_flags & SHF_ALLOC) ||
  780. (sechdrs[i].sh_type == SHT_MIPS_REGINFO)) {
  781. printk( KERN_DEBUG " ignoring section, "
  782. "name %s type %x address 0x%x \n",
  783. secstrings + sechdrs[i].sh_name,
  784. sechdrs[i].sh_type, sechdrs[i].sh_addr);
  785. continue;
  786. }
  787. if (sechdrs[i].sh_addr < (unsigned int)v->load_addr) {
  788. printk( KERN_WARNING "VPE loader: "
  789. "fully linked image has invalid section, "
  790. "name %s type %x address 0x%x, before load "
  791. "address of 0x%x\n",
  792. secstrings + sechdrs[i].sh_name,
  793. sechdrs[i].sh_type, sechdrs[i].sh_addr,
  794. (unsigned int)v->load_addr);
  795. return -ENOEXEC;
  796. }
  797. printk(KERN_DEBUG " copying section sh_name %s, sh_addr 0x%x "
  798. "size 0x%x0 from x%p\n",
  799. secstrings + sechdrs[i].sh_name, sechdrs[i].sh_addr,
  800. sechdrs[i].sh_size, hdr + sechdrs[i].sh_offset);
  801. if (sechdrs[i].sh_type != SHT_NOBITS)
  802. memcpy((void *)sechdrs[i].sh_addr,
  803. (char *)hdr + sechdrs[i].sh_offset,
  804. sechdrs[i].sh_size);
  805. else
  806. memset((void *)sechdrs[i].sh_addr, 0, sechdrs[i].sh_size);
  807. }
  808. }
  809. /* make sure it's physically written out */
  810. flush_icache_range((unsigned long)v->load_addr,
  811. (unsigned long)v->load_addr + v->len);
  812. if ((find_vpe_symbols(v, sechdrs, symindex, strtab, &mod)) < 0) {
  813. if (v->__start == 0) {
  814. printk(KERN_WARNING "VPE loader: program does not contain "
  815. "a __start symbol\n");
  816. return -ENOEXEC;
  817. }
  818. if (v->shared_ptr == NULL)
  819. printk(KERN_WARNING "VPE loader: "
  820. "program does not contain vpe_shared symbol.\n"
  821. " Unable to use AMVP (AP/SP) facilities.\n");
  822. }
  823. printk(" elf loaded\n");
  824. return 0;
  825. }
  826. __attribute_used__ void dump_vpe(struct vpe * v)
  827. {
  828. struct tc *t;
  829. settc(v->minor);
  830. printk(KERN_DEBUG "VPEControl 0x%lx\n", read_vpe_c0_vpecontrol());
  831. printk(KERN_DEBUG "VPEConf0 0x%lx\n", read_vpe_c0_vpeconf0());
  832. list_for_each_entry(t, &vpecontrol.tc_list, list)
  833. dump_tc(t);
  834. }
  835. static void cleanup_tc(struct tc *tc)
  836. {
  837. int tmp;
  838. /* Put MVPE's into 'configuration state' */
  839. set_c0_mvpcontrol(MVPCONTROL_VPC);
  840. settc(tc->index);
  841. tmp = read_tc_c0_tcstatus();
  842. /* mark not allocated and not dynamically allocatable */
  843. tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
  844. tmp |= TCSTATUS_IXMT; /* interrupt exempt */
  845. write_tc_c0_tcstatus(tmp);
  846. write_tc_c0_tchalt(TCHALT_H);
  847. /* bind it to anything other than VPE1 */
  848. write_tc_c0_tcbind(read_tc_c0_tcbind() & ~TCBIND_CURVPE); // | TCBIND_CURVPE
  849. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  850. }
  851. static int getcwd(char *buff, int size)
  852. {
  853. mm_segment_t old_fs;
  854. int ret;
  855. old_fs = get_fs();
  856. set_fs(KERNEL_DS);
  857. ret = sys_getcwd(buff,size);
  858. set_fs(old_fs);
  859. return ret;
  860. }
  861. /* checks VPE is unused and gets ready to load program */
  862. static int vpe_open(struct inode *inode, struct file *filp)
  863. {
  864. int minor, ret;
  865. struct vpe *v;
  866. struct vpe_notifications *not;
  867. /* assume only 1 device at the mo. */
  868. if ((minor = iminor(inode)) != 1) {
  869. printk(KERN_WARNING "VPE loader: only vpe1 is supported\n");
  870. return -ENODEV;
  871. }
  872. if ((v = get_vpe(minor)) == NULL) {
  873. printk(KERN_WARNING "VPE loader: unable to get vpe\n");
  874. return -ENODEV;
  875. }
  876. if (v->state != VPE_STATE_UNUSED) {
  877. dvpe();
  878. printk(KERN_DEBUG "VPE loader: tc in use dumping regs\n");
  879. dump_tc(get_tc(minor));
  880. list_for_each_entry(not, &v->notify, list) {
  881. not->stop(minor);
  882. }
  883. release_progmem(v->load_addr);
  884. cleanup_tc(get_tc(minor));
  885. }
  886. // allocate it so when we get write ops we know it's expected.
  887. v->state = VPE_STATE_INUSE;
  888. /* this of-course trashes what was there before... */
  889. v->pbuffer = vmalloc(P_SIZE);
  890. v->plen = P_SIZE;
  891. v->load_addr = NULL;
  892. v->len = 0;
  893. v->uid = filp->f_uid;
  894. v->gid = filp->f_gid;
  895. #ifdef CONFIG_MIPS_APSP_KSPD
  896. /* get kspd to tell us when a syscall_exit happens */
  897. if (!kspd_events_reqd) {
  898. kspd_notify(&kspd_events);
  899. kspd_events_reqd++;
  900. }
  901. #endif
  902. v->cwd[0] = 0;
  903. ret = getcwd(v->cwd, VPE_PATH_MAX);
  904. if (ret < 0)
  905. printk(KERN_WARNING "VPE loader: open, getcwd returned %d\n", ret);
  906. v->shared_ptr = NULL;
  907. v->__start = 0;
  908. return 0;
  909. }
  910. static int vpe_release(struct inode *inode, struct file *filp)
  911. {
  912. int minor, ret = 0;
  913. struct vpe *v;
  914. Elf_Ehdr *hdr;
  915. minor = iminor(inode);
  916. if ((v = get_vpe(minor)) == NULL)
  917. return -ENODEV;
  918. // simple case of fire and forget, so tell the VPE to run...
  919. hdr = (Elf_Ehdr *) v->pbuffer;
  920. if (memcmp(hdr->e_ident, ELFMAG, 4) == 0) {
  921. if (vpe_elfload(v) >= 0)
  922. vpe_run(v);
  923. else {
  924. printk(KERN_WARNING "VPE loader: ELF load failed.\n");
  925. ret = -ENOEXEC;
  926. }
  927. } else {
  928. printk(KERN_WARNING "VPE loader: only elf files are supported\n");
  929. ret = -ENOEXEC;
  930. }
  931. /* It's good to be able to run the SP and if it chokes have a look at
  932. the /dev/rt?. But if we reset the pointer to the shared struct we
  933. loose what has happened. So perhaps if garbage is sent to the vpe
  934. device, use it as a trigger for the reset. Hopefully a nice
  935. executable will be along shortly. */
  936. if (ret < 0)
  937. v->shared_ptr = NULL;
  938. // cleanup any temp buffers
  939. if (v->pbuffer)
  940. vfree(v->pbuffer);
  941. v->plen = 0;
  942. return ret;
  943. }
  944. static ssize_t vpe_write(struct file *file, const char __user * buffer,
  945. size_t count, loff_t * ppos)
  946. {
  947. int minor;
  948. size_t ret = count;
  949. struct vpe *v;
  950. minor = iminor(file->f_dentry->d_inode);
  951. if ((v = get_vpe(minor)) == NULL)
  952. return -ENODEV;
  953. if (v->pbuffer == NULL) {
  954. printk(KERN_ERR "VPE loader: no buffer for program\n");
  955. return -ENOMEM;
  956. }
  957. if ((count + v->len) > v->plen) {
  958. printk(KERN_WARNING
  959. "VPE loader: elf size too big. Perhaps strip uneeded symbols\n");
  960. return -ENOMEM;
  961. }
  962. count -= copy_from_user(v->pbuffer + v->len, buffer, count);
  963. if (!count)
  964. return -EFAULT;
  965. v->len += count;
  966. return ret;
  967. }
  968. static struct file_operations vpe_fops = {
  969. .owner = THIS_MODULE,
  970. .open = vpe_open,
  971. .release = vpe_release,
  972. .write = vpe_write
  973. };
  974. /* module wrapper entry points */
  975. /* give me a vpe */
  976. vpe_handle vpe_alloc(void)
  977. {
  978. int i;
  979. struct vpe *v;
  980. /* find a vpe */
  981. for (i = 1; i < MAX_VPES; i++) {
  982. if ((v = get_vpe(i)) != NULL) {
  983. v->state = VPE_STATE_INUSE;
  984. return v;
  985. }
  986. }
  987. return NULL;
  988. }
  989. EXPORT_SYMBOL(vpe_alloc);
  990. /* start running from here */
  991. int vpe_start(vpe_handle vpe, unsigned long start)
  992. {
  993. struct vpe *v = vpe;
  994. v->__start = start;
  995. return vpe_run(v);
  996. }
  997. EXPORT_SYMBOL(vpe_start);
  998. /* halt it for now */
  999. int vpe_stop(vpe_handle vpe)
  1000. {
  1001. struct vpe *v = vpe;
  1002. struct tc *t;
  1003. unsigned int evpe_flags;
  1004. evpe_flags = dvpe();
  1005. if ((t = list_entry(v->tc.next, struct tc, tc)) != NULL) {
  1006. settc(t->index);
  1007. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~VPECONF0_VPA);
  1008. }
  1009. evpe(evpe_flags);
  1010. return 0;
  1011. }
  1012. EXPORT_SYMBOL(vpe_stop);
  1013. /* I've done with it thank you */
  1014. int vpe_free(vpe_handle vpe)
  1015. {
  1016. struct vpe *v = vpe;
  1017. struct tc *t;
  1018. unsigned int evpe_flags;
  1019. if ((t = list_entry(v->tc.next, struct tc, tc)) == NULL) {
  1020. return -ENOEXEC;
  1021. }
  1022. evpe_flags = dvpe();
  1023. /* Put MVPE's into 'configuration state' */
  1024. set_c0_mvpcontrol(MVPCONTROL_VPC);
  1025. settc(t->index);
  1026. write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() & ~VPECONF0_VPA);
  1027. /* mark the TC unallocated and halt'ed */
  1028. write_tc_c0_tcstatus(read_tc_c0_tcstatus() & ~TCSTATUS_A);
  1029. write_tc_c0_tchalt(TCHALT_H);
  1030. v->state = VPE_STATE_UNUSED;
  1031. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  1032. evpe(evpe_flags);
  1033. return 0;
  1034. }
  1035. EXPORT_SYMBOL(vpe_free);
  1036. void *vpe_get_shared(int index)
  1037. {
  1038. struct vpe *v;
  1039. if ((v = get_vpe(index)) == NULL)
  1040. return NULL;
  1041. return v->shared_ptr;
  1042. }
  1043. EXPORT_SYMBOL(vpe_get_shared);
  1044. int vpe_getuid(int index)
  1045. {
  1046. struct vpe *v;
  1047. if ((v = get_vpe(index)) == NULL)
  1048. return -1;
  1049. return v->uid;
  1050. }
  1051. EXPORT_SYMBOL(vpe_getuid);
  1052. int vpe_getgid(int index)
  1053. {
  1054. struct vpe *v;
  1055. if ((v = get_vpe(index)) == NULL)
  1056. return -1;
  1057. return v->gid;
  1058. }
  1059. EXPORT_SYMBOL(vpe_getgid);
  1060. int vpe_notify(int index, struct vpe_notifications *notify)
  1061. {
  1062. struct vpe *v;
  1063. if ((v = get_vpe(index)) == NULL)
  1064. return -1;
  1065. list_add(&notify->list, &v->notify);
  1066. return 0;
  1067. }
  1068. EXPORT_SYMBOL(vpe_notify);
  1069. char *vpe_getcwd(int index)
  1070. {
  1071. struct vpe *v;
  1072. if ((v = get_vpe(index)) == NULL)
  1073. return NULL;
  1074. return v->cwd;
  1075. }
  1076. EXPORT_SYMBOL(vpe_getcwd);
  1077. #ifdef CONFIG_MIPS_APSP_KSPD
  1078. static void kspd_sp_exit( int sp_id)
  1079. {
  1080. cleanup_tc(get_tc(sp_id));
  1081. }
  1082. #endif
  1083. static int __init vpe_module_init(void)
  1084. {
  1085. struct vpe *v = NULL;
  1086. struct tc *t;
  1087. unsigned long val;
  1088. int i;
  1089. if (!cpu_has_mipsmt) {
  1090. printk("VPE loader: not a MIPS MT capable processor\n");
  1091. return -ENODEV;
  1092. }
  1093. major = register_chrdev(0, module_name, &vpe_fops);
  1094. if (major < 0) {
  1095. printk("VPE loader: unable to register character device\n");
  1096. return major;
  1097. }
  1098. dmt();
  1099. dvpe();
  1100. /* Put MVPE's into 'configuration state' */
  1101. set_c0_mvpcontrol(MVPCONTROL_VPC);
  1102. /* dump_mtregs(); */
  1103. INIT_LIST_HEAD(&vpecontrol.vpe_list);
  1104. INIT_LIST_HEAD(&vpecontrol.tc_list);
  1105. val = read_c0_mvpconf0();
  1106. for (i = 0; i < ((val & MVPCONF0_PTC) + 1); i++) {
  1107. t = alloc_tc(i);
  1108. /* VPE's */
  1109. if (i < ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1) {
  1110. settc(i);
  1111. if ((v = alloc_vpe(i)) == NULL) {
  1112. printk(KERN_WARNING "VPE: unable to allocate VPE\n");
  1113. return -ENODEV;
  1114. }
  1115. /* add the tc to the list of this vpe's tc's. */
  1116. list_add(&t->tc, &v->tc);
  1117. /* deactivate all but vpe0 */
  1118. if (i != 0) {
  1119. unsigned long tmp = read_vpe_c0_vpeconf0();
  1120. tmp &= ~VPECONF0_VPA;
  1121. /* master VPE */
  1122. tmp |= VPECONF0_MVP;
  1123. write_vpe_c0_vpeconf0(tmp);
  1124. }
  1125. /* disable multi-threading with TC's */
  1126. write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() & ~VPECONTROL_TE);
  1127. if (i != 0) {
  1128. write_vpe_c0_status((read_c0_status() &
  1129. ~(ST0_IM | ST0_IE | ST0_KSU))
  1130. | ST0_CU0);
  1131. /*
  1132. * Set config to be the same as vpe0,
  1133. * particularly kseg0 coherency alg
  1134. */
  1135. write_vpe_c0_config(read_c0_config());
  1136. }
  1137. }
  1138. /* TC's */
  1139. t->pvpe = v; /* set the parent vpe */
  1140. if (i != 0) {
  1141. unsigned long tmp;
  1142. settc(i);
  1143. /* Any TC that is bound to VPE0 gets left as is - in case
  1144. we are running SMTC on VPE0. A TC that is bound to any
  1145. other VPE gets bound to VPE0, ideally I'd like to make
  1146. it homeless but it doesn't appear to let me bind a TC
  1147. to a non-existent VPE. Which is perfectly reasonable.
  1148. The (un)bound state is visible to an EJTAG probe so may
  1149. notify GDB...
  1150. */
  1151. if (((tmp = read_tc_c0_tcbind()) & TCBIND_CURVPE)) {
  1152. /* tc is bound >vpe0 */
  1153. write_tc_c0_tcbind(tmp & ~TCBIND_CURVPE);
  1154. t->pvpe = get_vpe(0); /* set the parent vpe */
  1155. }
  1156. tmp = read_tc_c0_tcstatus();
  1157. /* mark not activated and not dynamically allocatable */
  1158. tmp &= ~(TCSTATUS_A | TCSTATUS_DA);
  1159. tmp |= TCSTATUS_IXMT; /* interrupt exempt */
  1160. write_tc_c0_tcstatus(tmp);
  1161. write_tc_c0_tchalt(TCHALT_H);
  1162. }
  1163. }
  1164. /* release config state */
  1165. clear_c0_mvpcontrol(MVPCONTROL_VPC);
  1166. #ifdef CONFIG_MIPS_APSP_KSPD
  1167. kspd_events.kspd_sp_exit = kspd_sp_exit;
  1168. #endif
  1169. return 0;
  1170. }
  1171. static void __exit vpe_module_exit(void)
  1172. {
  1173. struct vpe *v, *n;
  1174. list_for_each_entry_safe(v, n, &vpecontrol.vpe_list, list) {
  1175. if (v->state != VPE_STATE_UNUSED) {
  1176. release_vpe(v);
  1177. }
  1178. }
  1179. unregister_chrdev(major, module_name);
  1180. }
  1181. module_init(vpe_module_init);
  1182. module_exit(vpe_module_exit);
  1183. MODULE_DESCRIPTION("MIPS VPE Loader");
  1184. MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
  1185. MODULE_LICENSE("GPL");