vpe.c 37 KB

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