vpe.c 36 KB

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