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
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/spinlock.h>
13 #include <linux/list.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/bootmem.h>
18 #include <asm/ebcdic.h>
19 #include <asm/errno.h>
20 #include <asm/extmem.h>
21 #include <asm/cpcmd.h>
22 #include <linux/ctype.h>
24 #define DCSS_DEBUG /* Debug messages on/off */
26 #define DCSS_NAME "extmem"
28 #define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSS_NAME " debug:" x)
30 #define PRINT_DEBUG(x...) do {} while (0)
32 #define PRINT_INFO(x...) printk(KERN_INFO DCSS_NAME " info:" x)
33 #define PRINT_WARN(x...) printk(KERN_WARNING DCSS_NAME " warning:" x)
34 #define PRINT_ERR(x...) printk(KERN_ERR DCSS_NAME " error:" x)
37 #define DCSS_LOADSHR 0x00
38 #define DCSS_LOADNSR 0x04
39 #define DCSS_PURGESEG 0x08
40 #define DCSS_FINDSEG 0x0c
41 #define DCSS_LOADNOLY 0x10
42 #define DCSS_SEGEXT 0x18
43 #define DCSS_FINDSEGA 0x0c
46 unsigned int start; // 3byte start address, 1 byte type
47 unsigned int end; // 3byte end address, 1 byte reserved
55 struct qrange range[6];
69 struct list_head list;
71 unsigned long start_addr;
75 unsigned int vm_segtype;
76 struct qrange range[6];
80 static DEFINE_SPINLOCK(dcss_lock);
81 static struct list_head dcss_list = LIST_HEAD_INIT(dcss_list);
82 static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
86 unsigned long addr, size, type;
87 } memory_chunk[MEMORY_CHUNKS];
90 * Create the 8 bytes, ebcdic VM segment name from
94 dcss_mkname(char *name, char *dcss_name)
98 for (i = 0; i < 8; i++) {
101 dcss_name[i] = toupper(name[i]);
105 ASCEBC(dcss_name, 8);
110 * search all segments in dcss_list, and return the one
111 * namend *name. If not found, return NULL.
113 static struct dcss_segment *
114 segment_by_name (char *name)
118 struct dcss_segment *tmp, *retval = NULL;
120 assert_spin_locked(&dcss_lock);
121 dcss_mkname (name, dcss_name);
122 list_for_each (l, &dcss_list) {
123 tmp = list_entry (l, struct dcss_segment, list);
124 if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
134 * Perform a function on a dcss segment.
137 dcss_diag (__u8 func, void *parameter,
138 unsigned long *ret1, unsigned long *ret2)
140 unsigned long rx, ry;
143 rx = (unsigned long) parameter;
144 ry = (unsigned long) func;
155 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
162 dcss_diag_translate_rc (int vm_rc) {
169 /* do a diag to get info about a segment.
170 * fills start_address, end and vm_segtype fields
173 query_segment_type (struct dcss_segment *seg)
175 struct qin64 *qin = kmalloc (sizeof(struct qin64), GFP_DMA);
176 struct qout64 *qout = kmalloc (sizeof(struct qout64), GFP_DMA);
179 unsigned long dummy, vmrc;
181 if ((qin == NULL) || (qout == NULL)) {
186 /* initialize diag input parameters */
187 qin->qopcode = DCSS_FINDSEGA;
188 qin->qoutptr = (unsigned long) qout;
189 qin->qoutlen = sizeof(struct qout64);
190 memcpy (qin->qname, seg->dcss_name, 8);
192 diag_cc = dcss_diag (DCSS_SEGEXT, qin, &dummy, &vmrc);
195 PRINT_WARN ("segment_type: diag returned error %ld\n", vmrc);
196 rc = dcss_diag_translate_rc (vmrc);
200 if (qout->segcnt > 6) {
205 if (qout->segcnt == 1) {
206 seg->vm_segtype = qout->range[0].start & 0xff;
208 /* multi-part segment. only one type supported here:
209 - all parts are contiguous
210 - all parts are either EW or EN type
211 - maximum 6 parts allowed */
212 unsigned long start = qout->segstart >> PAGE_SHIFT;
213 for (i=0; i<qout->segcnt; i++) {
214 if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
215 ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
219 if (start != qout->range[i].start >> PAGE_SHIFT) {
223 start = (qout->range[i].end >> PAGE_SHIFT) + 1;
225 seg->vm_segtype = SEG_TYPE_EWEN;
228 /* analyze diag output and update seg */
229 seg->start_addr = qout->segstart;
230 seg->end = qout->segend;
232 memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
233 seg->segcnt = qout->segcnt;
244 * check if the given segment collides with guest storage.
245 * returns 1 if this is the case, 0 if no collision was found
248 segment_overlaps_storage(struct dcss_segment *seg)
252 for (i=0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
253 if (memory_chunk[i].type != 0)
255 if ((memory_chunk[i].addr >> 20) > (seg->end >> 20))
257 if (((memory_chunk[i].addr + memory_chunk[i].size - 1) >> 20)
258 < (seg->start_addr >> 20))
266 * check if segment collides with other segments that are currently loaded
267 * returns 1 if this is the case, 0 if no collision was found
270 segment_overlaps_others (struct dcss_segment *seg)
273 struct dcss_segment *tmp;
275 assert_spin_locked(&dcss_lock);
276 list_for_each(l, &dcss_list) {
277 tmp = list_entry(l, struct dcss_segment, list);
278 if ((tmp->start_addr >> 20) > (seg->end >> 20))
280 if ((tmp->end >> 20) < (seg->start_addr >> 20))
290 * check if segment exceeds the kernel mapping range (detected or set via mem=)
291 * returns 1 if this is the case, 0 if segment fits into the range
294 segment_exceeds_range (struct dcss_segment *seg)
296 int seg_last_pfn = (seg->end) >> PAGE_SHIFT;
297 if (seg_last_pfn > max_pfn)
303 * get info about a segment
304 * possible return values:
305 * -ENOSYS : we are not running on VM
306 * -EIO : could not perform query diagnose
307 * -ENOENT : no such segment
308 * -ENOTSUPP: multi-part segment cannot be used with linux
309 * -ENOSPC : segment cannot be used (overlaps with storage)
310 * -ENOMEM : out of memory
311 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
314 segment_type (char* name)
317 struct dcss_segment seg;
322 dcss_mkname(name, seg.dcss_name);
323 rc = query_segment_type (&seg);
326 return seg.vm_segtype;
330 * real segment loading function, called from segment_load
333 __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
335 struct dcss_segment *seg = kmalloc(sizeof(struct dcss_segment),
337 int dcss_command, rc, diag_cc;
343 dcss_mkname (name, seg->dcss_name);
344 rc = query_segment_type (seg);
347 if (segment_exceeds_range(seg)) {
348 PRINT_WARN ("segment_load: not loading segment %s - exceeds"
349 " kernel mapping range\n",name);
353 if (segment_overlaps_storage(seg)) {
354 PRINT_WARN ("segment_load: not loading segment %s - overlaps"
359 if (segment_overlaps_others(seg)) {
360 PRINT_WARN ("segment_load: not loading segment %s - overlaps"
361 " other segments\n",name);
366 dcss_command = DCSS_LOADNSR;
368 dcss_command = DCSS_LOADNOLY;
370 diag_cc = dcss_diag(dcss_command, seg->dcss_name,
371 &seg->start_addr, &seg->end);
373 PRINT_WARN ("segment_load: could not load segment %s - "
374 "diag returned error (%ld)\n",name,seg->end);
375 rc = dcss_diag_translate_rc (seg->end);
376 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
377 &seg->start_addr, &seg->end);
380 seg->do_nonshared = do_nonshared;
381 atomic_set(&seg->ref_count, 1);
382 list_add(&seg->list, &dcss_list);
383 rc = seg->vm_segtype;
384 *addr = seg->start_addr;
387 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
388 "type %s in non-shared mode\n", name,
389 (void*)seg->start_addr, (void*)seg->end,
390 segtype_string[seg->vm_segtype]);
392 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
393 "type %s in shared mode\n", name,
394 (void*)seg->start_addr, (void*)seg->end,
395 segtype_string[seg->vm_segtype]);
404 * this function loads a DCSS segment
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 * addr : will be filled with start address of the segment
409 * end : will be filled with end address of the segment
411 * -ENOSYS : we are not running on VM
412 * -EIO : could not perform query or load diagnose
413 * -ENOENT : no such segment
414 * -ENOTSUPP: multi-part segment cannot be used with linux
415 * -ENOSPC : segment cannot be used (overlaps with storage)
416 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
417 * -ERANGE : segment cannot be used (exceeds kernel mapping range)
418 * -EPERM : segment is currently loaded with incompatible permissions
419 * -ENOMEM : out of memory
420 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
423 segment_load (char *name, int do_nonshared, unsigned long *addr,
426 struct dcss_segment *seg;
432 spin_lock (&dcss_lock);
433 seg = segment_by_name (name);
435 rc = __segment_load (name, do_nonshared, addr, end);
437 if (do_nonshared == seg->do_nonshared) {
438 atomic_inc(&seg->ref_count);
439 *addr = seg->start_addr;
441 rc = seg->vm_segtype;
447 spin_unlock (&dcss_lock);
452 * this function modifies the shared state of a DCSS segment. note that
453 * name : name of the DCSS
454 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
455 * 1 indicates that the dcss should be exclusive for this linux image
457 * -EIO : could not perform load diagnose (segment gone!)
458 * -ENOENT : no such segment (segment gone!)
459 * -EAGAIN : segment is in use by other exploiters, try later
460 * -EINVAL : no segment with the given name is currently loaded - name invalid
461 * 0 : operation succeeded
464 segment_modify_shared (char *name, int do_nonshared)
466 struct dcss_segment *seg;
468 int dcss_command, rc, diag_cc;
470 spin_lock (&dcss_lock);
471 seg = segment_by_name (name);
476 if (do_nonshared == seg->do_nonshared) {
477 PRINT_INFO ("segment_modify_shared: not reloading segment %s"
478 " - already in requested mode\n",name);
482 if (atomic_read (&seg->ref_count) != 1) {
483 PRINT_WARN ("segment_modify_shared: not reloading segment %s - "
484 "segment is in use by other driver(s)\n",name);
488 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
491 dcss_command = DCSS_LOADNSR;
493 dcss_command = DCSS_LOADNOLY;
494 diag_cc = dcss_diag(dcss_command, seg->dcss_name,
495 &seg->start_addr, &seg->end);
497 PRINT_WARN ("segment_modify_shared: could not reload segment %s"
498 " - diag returned error (%ld)\n",name,seg->end);
499 rc = dcss_diag_translate_rc (seg->end);
502 seg->do_nonshared = do_nonshared;
506 list_del(&seg->list);
507 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
511 spin_unlock(&dcss_lock);
516 * Decrease the use count of a DCSS segment and remove
517 * it from the address space if nobody is using it
521 segment_unload(char *name)
524 struct dcss_segment *seg;
529 spin_lock(&dcss_lock);
530 seg = segment_by_name (name);
532 PRINT_ERR ("could not find segment %s in segment_unload, "
533 "please report to linux390@de.ibm.com\n",name);
536 if (atomic_dec_return(&seg->ref_count) == 0) {
537 list_del(&seg->list);
538 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
543 spin_unlock(&dcss_lock);
547 * save segment content permanently
550 segment_save(char *name)
552 struct dcss_segment *seg;
562 spin_lock(&dcss_lock);
563 seg = segment_by_name (name);
566 PRINT_ERR ("could not find segment %s in segment_save, please report to linux390@de.ibm.com\n",name);
570 startpfn = seg->start_addr >> PAGE_SHIFT;
571 endpfn = (seg->end) >> PAGE_SHIFT;
572 sprintf(cmd1, "DEFSEG %s", name);
573 for (i=0; i<seg->segcnt; i++) {
574 sprintf(cmd1+strlen(cmd1), " %X-%X %s",
575 seg->range[i].start >> PAGE_SHIFT,
576 seg->range[i].end >> PAGE_SHIFT,
577 segtype_string[seg->range[i].start & 0xff]);
579 sprintf(cmd2, "SAVESEG %s", name);
581 cpcmd(cmd1, NULL, 0, &response);
583 PRINT_ERR("segment_save: DEFSEG failed with response code %i\n",
587 cpcmd(cmd2, NULL, 0, &response);
589 PRINT_ERR("segment_save: SAVESEG failed with response code %i\n",
594 spin_unlock(&dcss_lock);
597 EXPORT_SYMBOL(segment_load);
598 EXPORT_SYMBOL(segment_unload);
599 EXPORT_SYMBOL(segment_save);
600 EXPORT_SYMBOL(segment_type);
601 EXPORT_SYMBOL(segment_modify_shared);