extmem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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"
  129. " diag %0,%1,0x64\n"
  130. " sam64\n"
  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. PRINT_WARN ("segment_type: diag returned error %ld\n", vmrc);
  169. rc = dcss_diag_translate_rc (vmrc);
  170. goto out_free;
  171. }
  172. if (qout->segcnt > 6) {
  173. rc = -ENOTSUPP;
  174. goto out_free;
  175. }
  176. if (qout->segcnt == 1) {
  177. seg->vm_segtype = qout->range[0].start & 0xff;
  178. } else {
  179. /* multi-part segment. only one type supported here:
  180. - all parts are contiguous
  181. - all parts are either EW or EN type
  182. - maximum 6 parts allowed */
  183. unsigned long start = qout->segstart >> PAGE_SHIFT;
  184. for (i=0; i<qout->segcnt; i++) {
  185. if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
  186. ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
  187. rc = -ENOTSUPP;
  188. goto out_free;
  189. }
  190. if (start != qout->range[i].start >> PAGE_SHIFT) {
  191. rc = -ENOTSUPP;
  192. goto out_free;
  193. }
  194. start = (qout->range[i].end >> PAGE_SHIFT) + 1;
  195. }
  196. seg->vm_segtype = SEG_TYPE_EWEN;
  197. }
  198. /* analyze diag output and update seg */
  199. seg->start_addr = qout->segstart;
  200. seg->end = qout->segend;
  201. memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
  202. seg->segcnt = qout->segcnt;
  203. rc = 0;
  204. out_free:
  205. kfree(qin);
  206. kfree(qout);
  207. return rc;
  208. }
  209. /*
  210. * check if the given segment collides with guest storage.
  211. * returns 1 if this is the case, 0 if no collision was found
  212. */
  213. static int
  214. segment_overlaps_storage(struct dcss_segment *seg)
  215. {
  216. int i;
  217. for (i=0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
  218. if (memory_chunk[i].type != 0)
  219. continue;
  220. if ((memory_chunk[i].addr >> 20) > (seg->end >> 20))
  221. continue;
  222. if (((memory_chunk[i].addr + memory_chunk[i].size - 1) >> 20)
  223. < (seg->start_addr >> 20))
  224. continue;
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. /*
  230. * check if segment collides with other segments that are currently loaded
  231. * returns 1 if this is the case, 0 if no collision was found
  232. */
  233. static int
  234. segment_overlaps_others (struct dcss_segment *seg)
  235. {
  236. struct list_head *l;
  237. struct dcss_segment *tmp;
  238. assert_spin_locked(&dcss_lock);
  239. list_for_each(l, &dcss_list) {
  240. tmp = list_entry(l, struct dcss_segment, list);
  241. if ((tmp->start_addr >> 20) > (seg->end >> 20))
  242. continue;
  243. if ((tmp->end >> 20) < (seg->start_addr >> 20))
  244. continue;
  245. if (seg == tmp)
  246. continue;
  247. return 1;
  248. }
  249. return 0;
  250. }
  251. /*
  252. * check if segment exceeds the kernel mapping range (detected or set via mem=)
  253. * returns 1 if this is the case, 0 if segment fits into the range
  254. */
  255. static inline int
  256. segment_exceeds_range (struct dcss_segment *seg)
  257. {
  258. int seg_last_pfn = (seg->end) >> PAGE_SHIFT;
  259. if (seg_last_pfn > max_pfn)
  260. return 1;
  261. return 0;
  262. }
  263. /*
  264. * get info about a segment
  265. * possible return values:
  266. * -ENOSYS : we are not running on VM
  267. * -EIO : could not perform query diagnose
  268. * -ENOENT : no such segment
  269. * -ENOTSUPP: multi-part segment cannot be used with linux
  270. * -ENOSPC : segment cannot be used (overlaps with storage)
  271. * -ENOMEM : out of memory
  272. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  273. */
  274. int
  275. segment_type (char* name)
  276. {
  277. int rc;
  278. struct dcss_segment seg;
  279. if (!MACHINE_IS_VM)
  280. return -ENOSYS;
  281. dcss_mkname(name, seg.dcss_name);
  282. rc = query_segment_type (&seg);
  283. if (rc < 0)
  284. return rc;
  285. return seg.vm_segtype;
  286. }
  287. /*
  288. * real segment loading function, called from segment_load
  289. */
  290. static int
  291. __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
  292. {
  293. struct dcss_segment *seg = kmalloc(sizeof(struct dcss_segment),
  294. GFP_DMA);
  295. int dcss_command, rc, diag_cc;
  296. if (seg == NULL) {
  297. rc = -ENOMEM;
  298. goto out;
  299. }
  300. dcss_mkname (name, seg->dcss_name);
  301. rc = query_segment_type (seg);
  302. if (rc < 0)
  303. goto out_free;
  304. if (segment_exceeds_range(seg)) {
  305. PRINT_WARN ("segment_load: not loading segment %s - exceeds"
  306. " kernel mapping range\n",name);
  307. rc = -ERANGE;
  308. goto out_free;
  309. }
  310. if (segment_overlaps_storage(seg)) {
  311. PRINT_WARN ("segment_load: not loading segment %s - overlaps"
  312. " storage\n",name);
  313. rc = -ENOSPC;
  314. goto out_free;
  315. }
  316. if (segment_overlaps_others(seg)) {
  317. PRINT_WARN ("segment_load: not loading segment %s - overlaps"
  318. " other segments\n",name);
  319. rc = -EBUSY;
  320. goto out_free;
  321. }
  322. if (do_nonshared)
  323. dcss_command = DCSS_LOADNSR;
  324. else
  325. dcss_command = DCSS_LOADNOLY;
  326. diag_cc = dcss_diag(dcss_command, seg->dcss_name,
  327. &seg->start_addr, &seg->end);
  328. if (diag_cc > 1) {
  329. PRINT_WARN ("segment_load: could not load segment %s - "
  330. "diag returned error (%ld)\n",name,seg->end);
  331. rc = dcss_diag_translate_rc (seg->end);
  332. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  333. &seg->start_addr, &seg->end);
  334. goto out_free;
  335. }
  336. seg->do_nonshared = do_nonshared;
  337. atomic_set(&seg->ref_count, 1);
  338. list_add(&seg->list, &dcss_list);
  339. rc = seg->vm_segtype;
  340. *addr = seg->start_addr;
  341. *end = seg->end;
  342. if (do_nonshared)
  343. PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
  344. "type %s in non-shared mode\n", name,
  345. (void*)seg->start_addr, (void*)seg->end,
  346. segtype_string[seg->vm_segtype]);
  347. else
  348. PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
  349. "type %s in shared mode\n", name,
  350. (void*)seg->start_addr, (void*)seg->end,
  351. segtype_string[seg->vm_segtype]);
  352. goto out;
  353. out_free:
  354. kfree(seg);
  355. out:
  356. return rc;
  357. }
  358. /*
  359. * this function loads a DCSS segment
  360. * name : name of the DCSS
  361. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  362. * 1 indicates that the dcss should be exclusive for this linux image
  363. * addr : will be filled with start address of the segment
  364. * end : will be filled with end address of the segment
  365. * return values:
  366. * -ENOSYS : we are not running on VM
  367. * -EIO : could not perform query or load diagnose
  368. * -ENOENT : no such segment
  369. * -ENOTSUPP: multi-part segment cannot be used with linux
  370. * -ENOSPC : segment cannot be used (overlaps with storage)
  371. * -EBUSY : segment can temporarily not be used (overlaps with dcss)
  372. * -ERANGE : segment cannot be used (exceeds kernel mapping range)
  373. * -EPERM : segment is currently loaded with incompatible permissions
  374. * -ENOMEM : out of memory
  375. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  376. */
  377. int
  378. segment_load (char *name, int do_nonshared, unsigned long *addr,
  379. unsigned long *end)
  380. {
  381. struct dcss_segment *seg;
  382. int rc;
  383. if (!MACHINE_IS_VM)
  384. return -ENOSYS;
  385. spin_lock (&dcss_lock);
  386. seg = segment_by_name (name);
  387. if (seg == NULL)
  388. rc = __segment_load (name, do_nonshared, addr, end);
  389. else {
  390. if (do_nonshared == seg->do_nonshared) {
  391. atomic_inc(&seg->ref_count);
  392. *addr = seg->start_addr;
  393. *end = seg->end;
  394. rc = seg->vm_segtype;
  395. } else {
  396. *addr = *end = 0;
  397. rc = -EPERM;
  398. }
  399. }
  400. spin_unlock (&dcss_lock);
  401. return rc;
  402. }
  403. /*
  404. * this function modifies the shared state of a DCSS segment. note that
  405. * name : name of the DCSS
  406. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  407. * 1 indicates that the dcss should be exclusive for this linux image
  408. * return values:
  409. * -EIO : could not perform load diagnose (segment gone!)
  410. * -ENOENT : no such segment (segment gone!)
  411. * -EAGAIN : segment is in use by other exploiters, try later
  412. * -EINVAL : no segment with the given name is currently loaded - name invalid
  413. * 0 : operation succeeded
  414. */
  415. int
  416. segment_modify_shared (char *name, int do_nonshared)
  417. {
  418. struct dcss_segment *seg;
  419. unsigned long dummy;
  420. int dcss_command, rc, diag_cc;
  421. spin_lock (&dcss_lock);
  422. seg = segment_by_name (name);
  423. if (seg == NULL) {
  424. rc = -EINVAL;
  425. goto out_unlock;
  426. }
  427. if (do_nonshared == seg->do_nonshared) {
  428. PRINT_INFO ("segment_modify_shared: not reloading segment %s"
  429. " - already in requested mode\n",name);
  430. rc = 0;
  431. goto out_unlock;
  432. }
  433. if (atomic_read (&seg->ref_count) != 1) {
  434. PRINT_WARN ("segment_modify_shared: not reloading segment %s - "
  435. "segment is in use by other driver(s)\n",name);
  436. rc = -EAGAIN;
  437. goto out_unlock;
  438. }
  439. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  440. &dummy, &dummy);
  441. if (do_nonshared)
  442. dcss_command = DCSS_LOADNSR;
  443. else
  444. dcss_command = DCSS_LOADNOLY;
  445. diag_cc = dcss_diag(dcss_command, seg->dcss_name,
  446. &seg->start_addr, &seg->end);
  447. if (diag_cc > 1) {
  448. PRINT_WARN ("segment_modify_shared: could not reload segment %s"
  449. " - diag returned error (%ld)\n",name,seg->end);
  450. rc = dcss_diag_translate_rc (seg->end);
  451. goto out_del;
  452. }
  453. seg->do_nonshared = do_nonshared;
  454. rc = 0;
  455. goto out_unlock;
  456. out_del:
  457. list_del(&seg->list);
  458. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  459. &dummy, &dummy);
  460. kfree(seg);
  461. out_unlock:
  462. spin_unlock(&dcss_lock);
  463. return rc;
  464. }
  465. /*
  466. * Decrease the use count of a DCSS segment and remove
  467. * it from the address space if nobody is using it
  468. * any longer.
  469. */
  470. void
  471. segment_unload(char *name)
  472. {
  473. unsigned long dummy;
  474. struct dcss_segment *seg;
  475. if (!MACHINE_IS_VM)
  476. return;
  477. spin_lock(&dcss_lock);
  478. seg = segment_by_name (name);
  479. if (seg == NULL) {
  480. PRINT_ERR ("could not find segment %s in segment_unload, "
  481. "please report to linux390@de.ibm.com\n",name);
  482. goto out_unlock;
  483. }
  484. if (atomic_dec_return(&seg->ref_count) == 0) {
  485. list_del(&seg->list);
  486. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  487. &dummy, &dummy);
  488. kfree(seg);
  489. }
  490. out_unlock:
  491. spin_unlock(&dcss_lock);
  492. }
  493. /*
  494. * save segment content permanently
  495. */
  496. void
  497. segment_save(char *name)
  498. {
  499. struct dcss_segment *seg;
  500. int startpfn = 0;
  501. int endpfn = 0;
  502. char cmd1[160];
  503. char cmd2[80];
  504. int i, response;
  505. if (!MACHINE_IS_VM)
  506. return;
  507. spin_lock(&dcss_lock);
  508. seg = segment_by_name (name);
  509. if (seg == NULL) {
  510. PRINT_ERR ("could not find segment %s in segment_save, please report to linux390@de.ibm.com\n",name);
  511. return;
  512. }
  513. startpfn = seg->start_addr >> PAGE_SHIFT;
  514. endpfn = (seg->end) >> PAGE_SHIFT;
  515. sprintf(cmd1, "DEFSEG %s", name);
  516. for (i=0; i<seg->segcnt; i++) {
  517. sprintf(cmd1+strlen(cmd1), " %X-%X %s",
  518. seg->range[i].start >> PAGE_SHIFT,
  519. seg->range[i].end >> PAGE_SHIFT,
  520. segtype_string[seg->range[i].start & 0xff]);
  521. }
  522. sprintf(cmd2, "SAVESEG %s", name);
  523. response = 0;
  524. cpcmd(cmd1, NULL, 0, &response);
  525. if (response) {
  526. PRINT_ERR("segment_save: DEFSEG failed with response code %i\n",
  527. response);
  528. goto out;
  529. }
  530. cpcmd(cmd2, NULL, 0, &response);
  531. if (response) {
  532. PRINT_ERR("segment_save: SAVESEG failed with response code %i\n",
  533. response);
  534. goto out;
  535. }
  536. out:
  537. spin_unlock(&dcss_lock);
  538. }
  539. EXPORT_SYMBOL(segment_load);
  540. EXPORT_SYMBOL(segment_unload);
  541. EXPORT_SYMBOL(segment_save);
  542. EXPORT_SYMBOL(segment_type);
  543. EXPORT_SYMBOL(segment_modify_shared);