vpe.c 35 KB

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