vpe.c 36 KB

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