extmem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * File...........: arch/s390/mm/extmem.c
  3. * Author(s)......: Carsten Otte <cotte@de.ibm.com>
  4. * Rob M van der Heij <rvdheij@nl.ibm.com>
  5. * Steven Shultz <shultzss@us.ibm.com>
  6. * Bugreports.to..: <Linux390@de.ibm.com>
  7. * (C) IBM Corporation 2002-2004
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/string.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/list.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/bootmem.h>
  16. #include <asm/page.h>
  17. #include <asm/ebcdic.h>
  18. #include <asm/errno.h>
  19. #include <asm/extmem.h>
  20. #include <asm/cpcmd.h>
  21. #include <linux/ctype.h>
  22. #define DCSS_DEBUG /* Debug messages on/off */
  23. #define DCSS_NAME "extmem"
  24. #ifdef DCSS_DEBUG
  25. #define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSS_NAME " debug:" x)
  26. #else
  27. #define PRINT_DEBUG(x...) do {} while (0)
  28. #endif
  29. #define PRINT_INFO(x...) printk(KERN_INFO DCSS_NAME " info:" x)
  30. #define PRINT_WARN(x...) printk(KERN_WARNING DCSS_NAME " warning:" x)
  31. #define PRINT_ERR(x...) printk(KERN_ERR DCSS_NAME " error:" x)
  32. #define DCSS_LOADSHR 0x00
  33. #define DCSS_LOADNSR 0x04
  34. #define DCSS_PURGESEG 0x08
  35. #define DCSS_FINDSEG 0x0c
  36. #define DCSS_LOADNOLY 0x10
  37. #define DCSS_SEGEXT 0x18
  38. #define DCSS_FINDSEGA 0x0c
  39. struct qrange {
  40. unsigned int start; // 3byte start address, 1 byte type
  41. unsigned int end; // 3byte end address, 1 byte reserved
  42. };
  43. struct qout64 {
  44. int segstart;
  45. int segend;
  46. int segcnt;
  47. int segrcnt;
  48. struct qrange range[6];
  49. };
  50. struct qin64 {
  51. char qopcode;
  52. char rsrv1[3];
  53. char qrcode;
  54. char rsrv2[3];
  55. char qname[8];
  56. unsigned int qoutptr;
  57. short int qoutlen;
  58. };
  59. struct dcss_segment {
  60. struct list_head list;
  61. char dcss_name[8];
  62. unsigned long start_addr;
  63. unsigned long end;
  64. atomic_t ref_count;
  65. int do_nonshared;
  66. unsigned int vm_segtype;
  67. struct qrange range[6];
  68. int segcnt;
  69. };
  70. static DEFINE_SPINLOCK(dcss_lock);
  71. static struct list_head dcss_list = LIST_HEAD_INIT(dcss_list);
  72. static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
  73. "EW/EN-MIXED" };
  74. extern struct {
  75. unsigned long addr, size, type;
  76. } memory_chunk[MEMORY_CHUNKS];
  77. /*
  78. * Create the 8 bytes, ebcdic VM segment name from
  79. * an ascii name.
  80. */
  81. static void inline
  82. dcss_mkname(char *name, char *dcss_name)
  83. {
  84. int i;
  85. for (i = 0; i < 8; i++) {
  86. if (name[i] == '\0')
  87. break;
  88. dcss_name[i] = toupper(name[i]);
  89. };
  90. for (; i < 8; i++)
  91. dcss_name[i] = ' ';
  92. ASCEBC(dcss_name, 8);
  93. }
  94. /*
  95. * search all segments in dcss_list, and return the one
  96. * namend *name. If not found, return NULL.
  97. */
  98. static struct dcss_segment *
  99. segment_by_name (char *name)
  100. {
  101. char dcss_name[9];
  102. struct list_head *l;
  103. struct dcss_segment *tmp, *retval = NULL;
  104. assert_spin_locked(&dcss_lock);
  105. dcss_mkname (name, dcss_name);
  106. list_for_each (l, &dcss_list) {
  107. tmp = list_entry (l, struct dcss_segment, list);
  108. if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
  109. retval = tmp;
  110. break;
  111. }
  112. }
  113. return retval;
  114. }
  115. /*
  116. * Perform a function on a dcss segment.
  117. */
  118. static inline int
  119. dcss_diag (__u8 func, void *parameter,
  120. unsigned long *ret1, unsigned long *ret2)
  121. {
  122. unsigned long rx, ry;
  123. int rc;
  124. rx = (unsigned long) parameter;
  125. ry = (unsigned long) func;
  126. __asm__ __volatile__(
  127. #ifdef CONFIG_64BIT
  128. " sam31\n" // switch to 31 bit
  129. " diag %0,%1,0x64\n"
  130. " sam64\n" // switch back to 64 bit
  131. #else
  132. " diag %0,%1,0x64\n"
  133. #endif
  134. " ipm %2\n"
  135. " srl %2,28\n"
  136. : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc" );
  137. *ret1 = rx;
  138. *ret2 = ry;
  139. return rc;
  140. }
  141. static inline int
  142. dcss_diag_translate_rc (int vm_rc) {
  143. if (vm_rc == 44)
  144. return -ENOENT;
  145. return -EIO;
  146. }
  147. /* do a diag to get info about a segment.
  148. * fills start_address, end and vm_segtype fields
  149. */
  150. static int
  151. query_segment_type (struct dcss_segment *seg)
  152. {
  153. struct qin64 *qin = kmalloc (sizeof(struct qin64), GFP_DMA);
  154. struct qout64 *qout = kmalloc (sizeof(struct qout64), GFP_DMA);
  155. int diag_cc, rc, i;
  156. unsigned long dummy, vmrc;
  157. if ((qin == NULL) || (qout == NULL)) {
  158. rc = -ENOMEM;
  159. goto out_free;
  160. }
  161. /* initialize diag input parameters */
  162. qin->qopcode = DCSS_FINDSEGA;
  163. qin->qoutptr = (unsigned long) qout;
  164. qin->qoutlen = sizeof(struct qout64);
  165. memcpy (qin->qname, seg->dcss_name, 8);
  166. diag_cc = dcss_diag (DCSS_SEGEXT, qin, &dummy, &vmrc);
  167. if (diag_cc > 1) {
  168. rc = dcss_diag_translate_rc (vmrc);
  169. goto out_free;
  170. }
  171. if (qout->segcnt > 6) {
  172. rc = -ENOTSUPP;
  173. goto out_free;
  174. }
  175. if (qout->segcnt == 1) {
  176. seg->vm_segtype = qout->range[0].start & 0xff;
  177. } else {
  178. /* multi-part segment. only one type supported here:
  179. - all parts are contiguous
  180. - all parts are either EW or EN type
  181. - maximum 6 parts allowed */
  182. unsigned long start = qout->segstart >> PAGE_SHIFT;
  183. for (i=0; i<qout->segcnt; i++) {
  184. if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
  185. ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
  186. rc = -ENOTSUPP;
  187. goto out_free;
  188. }
  189. if (start != qout->range[i].start >> PAGE_SHIFT) {
  190. rc = -ENOTSUPP;
  191. goto out_free;
  192. }
  193. start = (qout->range[i].end >> PAGE_SHIFT) + 1;
  194. }
  195. seg->vm_segtype = SEG_TYPE_EWEN;
  196. }
  197. /* analyze diag output and update seg */
  198. seg->start_addr = qout->segstart;
  199. seg->end = qout->segend;
  200. memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
  201. seg->segcnt = qout->segcnt;
  202. rc = 0;
  203. out_free:
  204. kfree(qin);
  205. kfree(qout);
  206. return rc;
  207. }
  208. /*
  209. * check if the given segment collides with guest storage.
  210. * returns 1 if this is the case, 0 if no collision was found
  211. */
  212. static int
  213. segment_overlaps_storage(struct dcss_segment *seg)
  214. {
  215. int i;
  216. for (i=0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
  217. if (memory_chunk[i].type != 0)
  218. continue;
  219. if ((memory_chunk[i].addr >> 20) > (seg->end >> 20))
  220. continue;
  221. if (((memory_chunk[i].addr + memory_chunk[i].size - 1) >> 20)
  222. < (seg->start_addr >> 20))
  223. continue;
  224. return 1;
  225. }
  226. return 0;
  227. }
  228. /*
  229. * check if segment collides with other segments that are currently loaded
  230. * returns 1 if this is the case, 0 if no collision was found
  231. */
  232. static int
  233. segment_overlaps_others (struct dcss_segment *seg)
  234. {
  235. struct list_head *l;
  236. struct dcss_segment *tmp;
  237. assert_spin_locked(&dcss_lock);
  238. list_for_each(l, &dcss_list) {
  239. tmp = list_entry(l, struct dcss_segment, list);
  240. if ((tmp->start_addr >> 20) > (seg->end >> 20))
  241. continue;
  242. if ((tmp->end >> 20) < (seg->start_addr >> 20))
  243. continue;
  244. if (seg == tmp)
  245. continue;
  246. return 1;
  247. }
  248. return 0;
  249. }
  250. /*
  251. * check if segment exceeds the kernel mapping range (detected or set via mem=)
  252. * returns 1 if this is the case, 0 if segment fits into the range
  253. */
  254. static inline int
  255. segment_exceeds_range (struct dcss_segment *seg)
  256. {
  257. int seg_last_pfn = (seg->end) >> PAGE_SHIFT;
  258. if (seg_last_pfn > max_pfn)
  259. return 1;
  260. return 0;
  261. }
  262. /*
  263. * get info about a segment
  264. * possible return values:
  265. * -ENOSYS : we are not running on VM
  266. * -EIO : could not perform query diagnose
  267. * -ENOENT : no such segment
  268. * -ENOTSUPP: multi-part segment cannot be used with linux
  269. * -ENOSPC : segment cannot be used (overlaps with storage)
  270. * -ENOMEM : out of memory
  271. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  272. */
  273. int
  274. segment_type (char* name)
  275. {
  276. int rc;
  277. struct dcss_segment seg;
  278. if (!MACHINE_IS_VM)
  279. return -ENOSYS;
  280. dcss_mkname(name, seg.dcss_name);
  281. rc = query_segment_type (&seg);
  282. if (rc < 0)
  283. return rc;
  284. return seg.vm_segtype;
  285. }
  286. /*
  287. * real segment loading function, called from segment_load
  288. */
  289. static int
  290. __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
  291. {
  292. struct dcss_segment *seg = kmalloc(sizeof(struct dcss_segment),
  293. GFP_DMA);
  294. int dcss_command, rc, diag_cc;
  295. if (seg == NULL) {
  296. rc = -ENOMEM;
  297. goto out;
  298. }
  299. dcss_mkname (name, seg->dcss_name);
  300. rc = query_segment_type (seg);
  301. if (rc < 0)
  302. goto out_free;
  303. if (segment_exceeds_range(seg)) {
  304. PRINT_WARN ("segment_load: not loading segment %s - exceeds"
  305. " kernel mapping range\n",name);
  306. rc = -ERANGE;
  307. goto out_free;
  308. }
  309. if (segment_overlaps_storage(seg)) {
  310. PRINT_WARN ("segment_load: not loading segment %s - overlaps"
  311. " storage\n",name);
  312. rc = -ENOSPC;
  313. goto out_free;
  314. }
  315. if (segment_overlaps_others(seg)) {
  316. PRINT_WARN ("segment_load: not loading segment %s - overlaps"
  317. " other segments\n",name);
  318. rc = -EBUSY;
  319. goto out_free;
  320. }
  321. if (do_nonshared)
  322. dcss_command = DCSS_LOADNSR;
  323. else
  324. dcss_command = DCSS_LOADNOLY;
  325. diag_cc = dcss_diag(dcss_command, seg->dcss_name,
  326. &seg->start_addr, &seg->end);
  327. if (diag_cc > 1) {
  328. PRINT_WARN ("segment_load: could not load segment %s - "
  329. "diag returned error (%ld)\n",name,seg->end);
  330. rc = dcss_diag_translate_rc (seg->end);
  331. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  332. &seg->start_addr, &seg->end);
  333. goto out_free;
  334. }
  335. seg->do_nonshared = do_nonshared;
  336. atomic_set(&seg->ref_count, 1);
  337. list_add(&seg->list, &dcss_list);
  338. rc = seg->vm_segtype;
  339. *addr = seg->start_addr;
  340. *end = seg->end;
  341. if (do_nonshared)
  342. PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
  343. "type %s in non-shared mode\n", name,
  344. (void*)seg->start_addr, (void*)seg->end,
  345. segtype_string[seg->vm_segtype]);
  346. else
  347. PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
  348. "type %s in shared mode\n", name,
  349. (void*)seg->start_addr, (void*)seg->end,
  350. segtype_string[seg->vm_segtype]);
  351. goto out;
  352. out_free:
  353. kfree(seg);
  354. out:
  355. return rc;
  356. }
  357. /*
  358. * this function loads a DCSS segment
  359. * name : name of the DCSS
  360. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  361. * 1 indicates that the dcss should be exclusive for this linux image
  362. * addr : will be filled with start address of the segment
  363. * end : will be filled with end address of the segment
  364. * return values:
  365. * -ENOSYS : we are not running on VM
  366. * -EIO : could not perform query or load diagnose
  367. * -ENOENT : no such segment
  368. * -ENOTSUPP: multi-part segment cannot be used with linux
  369. * -ENOSPC : segment cannot be used (overlaps with storage)
  370. * -EBUSY : segment can temporarily not be used (overlaps with dcss)
  371. * -ERANGE : segment cannot be used (exceeds kernel mapping range)
  372. * -EPERM : segment is currently loaded with incompatible permissions
  373. * -ENOMEM : out of memory
  374. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  375. */
  376. int
  377. segment_load (char *name, int do_nonshared, unsigned long *addr,
  378. unsigned long *end)
  379. {
  380. struct dcss_segment *seg;
  381. int rc;
  382. if (!MACHINE_IS_VM)
  383. return -ENOSYS;
  384. spin_lock (&dcss_lock);
  385. seg = segment_by_name (name);
  386. if (seg == NULL)
  387. rc = __segment_load (name, do_nonshared, addr, end);
  388. else {
  389. if (do_nonshared == seg->do_nonshared) {
  390. atomic_inc(&seg->ref_count);
  391. *addr = seg->start_addr;
  392. *end = seg->end;
  393. rc = seg->vm_segtype;
  394. } else {
  395. *addr = *end = 0;
  396. rc = -EPERM;
  397. }
  398. }
  399. spin_unlock (&dcss_lock);
  400. return rc;
  401. }
  402. /*
  403. * this function modifies the shared state of a DCSS segment. note that
  404. * name : name of the DCSS
  405. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  406. * 1 indicates that the dcss should be exclusive for this linux image
  407. * return values:
  408. * -EIO : could not perform load diagnose (segment gone!)
  409. * -ENOENT : no such segment (segment gone!)
  410. * -EAGAIN : segment is in use by other exploiters, try later
  411. * -EINVAL : no segment with the given name is currently loaded - name invalid
  412. * 0 : operation succeeded
  413. */
  414. int
  415. segment_modify_shared (char *name, int do_nonshared)
  416. {
  417. struct dcss_segment *seg;
  418. unsigned long dummy;
  419. int dcss_command, rc, diag_cc;
  420. spin_lock (&dcss_lock);
  421. seg = segment_by_name (name);
  422. if (seg == NULL) {
  423. rc = -EINVAL;
  424. goto out_unlock;
  425. }
  426. if (do_nonshared == seg->do_nonshared) {
  427. PRINT_INFO ("segment_modify_shared: not reloading segment %s"
  428. " - already in requested mode\n",name);
  429. rc = 0;
  430. goto out_unlock;
  431. }
  432. if (atomic_read (&seg->ref_count) != 1) {
  433. PRINT_WARN ("segment_modify_shared: not reloading segment %s - "
  434. "segment is in use by other driver(s)\n",name);
  435. rc = -EAGAIN;
  436. goto out_unlock;
  437. }
  438. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  439. &dummy, &dummy);
  440. if (do_nonshared)
  441. dcss_command = DCSS_LOADNSR;
  442. else
  443. dcss_command = DCSS_LOADNOLY;
  444. diag_cc = dcss_diag(dcss_command, seg->dcss_name,
  445. &seg->start_addr, &seg->end);
  446. if (diag_cc > 1) {
  447. PRINT_WARN ("segment_modify_shared: could not reload segment %s"
  448. " - diag returned error (%ld)\n",name,seg->end);
  449. rc = dcss_diag_translate_rc (seg->end);
  450. goto out_del;
  451. }
  452. seg->do_nonshared = do_nonshared;
  453. rc = 0;
  454. goto out_unlock;
  455. out_del:
  456. list_del(&seg->list);
  457. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  458. &dummy, &dummy);
  459. kfree(seg);
  460. out_unlock:
  461. spin_unlock(&dcss_lock);
  462. return rc;
  463. }
  464. /*
  465. * Decrease the use count of a DCSS segment and remove
  466. * it from the address space if nobody is using it
  467. * any longer.
  468. */
  469. void
  470. segment_unload(char *name)
  471. {
  472. unsigned long dummy;
  473. struct dcss_segment *seg;
  474. if (!MACHINE_IS_VM)
  475. return;
  476. spin_lock(&dcss_lock);
  477. seg = segment_by_name (name);
  478. if (seg == NULL) {
  479. PRINT_ERR ("could not find segment %s in segment_unload, "
  480. "please report to linux390@de.ibm.com\n",name);
  481. goto out_unlock;
  482. }
  483. if (atomic_dec_return(&seg->ref_count) == 0) {
  484. list_del(&seg->list);
  485. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  486. &dummy, &dummy);
  487. kfree(seg);
  488. }
  489. out_unlock:
  490. spin_unlock(&dcss_lock);
  491. }
  492. /*
  493. * save segment content permanently
  494. */
  495. void
  496. segment_save(char *name)
  497. {
  498. struct dcss_segment *seg;
  499. int startpfn = 0;
  500. int endpfn = 0;
  501. char cmd1[160];
  502. char cmd2[80];
  503. int i;
  504. if (!MACHINE_IS_VM)
  505. return;
  506. spin_lock(&dcss_lock);
  507. seg = segment_by_name (name);
  508. if (seg == NULL) {
  509. PRINT_ERR ("could not find segment %s in segment_save, please report to linux390@de.ibm.com\n",name);
  510. return;
  511. }
  512. startpfn = seg->start_addr >> PAGE_SHIFT;
  513. endpfn = (seg->end) >> PAGE_SHIFT;
  514. sprintf(cmd1, "DEFSEG %s", name);
  515. for (i=0; i<seg->segcnt; i++) {
  516. sprintf(cmd1+strlen(cmd1), " %X-%X %s",
  517. seg->range[i].start >> PAGE_SHIFT,
  518. seg->range[i].end >> PAGE_SHIFT,
  519. segtype_string[seg->range[i].start & 0xff]);
  520. }
  521. sprintf(cmd2, "SAVESEG %s", name);
  522. cpcmd(cmd1, NULL, 0, NULL);
  523. cpcmd(cmd2, NULL, 0, NULL);
  524. spin_unlock(&dcss_lock);
  525. }
  526. EXPORT_SYMBOL(segment_load);
  527. EXPORT_SYMBOL(segment_unload);
  528. EXPORT_SYMBOL(segment_save);
  529. EXPORT_SYMBOL(segment_type);
  530. EXPORT_SYMBOL(segment_modify_shared);