vpe.c 36 KB

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