vpe.c 35 KB

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