aboutsummaryrefslogtreecommitdiffstats
path: root/techlibs/anlogic
diff options
context:
space:
mode:
Diffstat (limited to 'techlibs/anlogic')
0 files changed, 0 insertions, 0 deletions
='n28' href='#n28'>28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
/*
 * fw_emul.c:
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 *
 */
#include <xen/config.h>
#include <asm/system.h>
#include <asm/pgalloc.h>

#include <linux/efi.h>
#include <asm/pal.h>
#include <asm/sal.h>
#include <asm/sn/sn_sal.h>
#include <asm/sn/hubdev.h>
#include <asm/xenmca.h>

#include <public/sched.h>
#include "hpsim_ssc.h"
#include <asm/vcpu.h>
#include <asm/vmx_vcpu.h>
#include <asm/dom_fw.h>
#include <asm/uaccess.h>
#include <xen/console.h>
#include <xen/hypercall.h>
#include <xen/softirq.h>
#include <xen/time.h>
#include <asm/debugger.h>
#include <asm/vmx_phy_mode.h>

static DEFINE_SPINLOCK(efi_time_services_lock);

struct sal_mc_params {
	u64 param_type;
	u64 i_or_m;
	u64 i_or_m_val;
	u64 timeout;
	u64 rz_always;
} sal_mc_params[SAL_MC_PARAM_CPE_INT + 1];

struct sal_vectors {
	u64 vector_type;
	u64 handler_addr1;
	u64 gp1;
	u64 handler_len1;
	u64 handler_addr2;
	u64 gp2;
	u64 handler_len2;
} sal_vectors[SAL_VECTOR_OS_BOOT_RENDEZ + 1];

struct smp_call_args_t {
	u64 type;
	u64 ret;
	u64 target;
	struct domain *domain;
	int corrected;
	int status;
	void *data;
}; 

extern sal_log_record_header_t	*sal_record;
DEFINE_SPINLOCK(sal_record_lock);

extern spinlock_t sal_queue_lock;

#define IA64_SAL_NO_INFORMATION_AVAILABLE	-5

#if defined(IA64_SAL_DEBUG_INFO)
static const char * const rec_name[] = { "MCA", "INIT", "CMC", "CPE" };

# define IA64_SAL_DEBUG(fmt...)	printk("sal_emulator: " fmt)
#else
# define IA64_SAL_DEBUG(fmt...)
#endif

void get_state_info_on(void *data) {
	struct smp_call_args_t *arg = data;
	int flags;

	spin_lock_irqsave(&sal_record_lock, flags);
	memset(sal_record, 0, ia64_sal_get_state_info_size(arg->type));
	arg->ret = ia64_sal_get_state_info(arg->type, (u64 *)sal_record);
	IA64_SAL_DEBUG("SAL_GET_STATE_INFO(%s) on CPU#%d returns %ld.\n",
	               rec_name[arg->type], smp_processor_id(), arg->ret);
	if (arg->corrected) {
		sal_record->severity = sal_log_severity_corrected;
		IA64_SAL_DEBUG("%s: IA64_SAL_CLEAR_STATE_INFO(SAL_INFO_TYPE_MCA)"
		               " force\n", __FUNCTION__);
	}
	if (arg->ret > 0) {
	  	/*
		 * Save current->domain and set to local(caller) domain for
		 * xencomm_paddr_to_maddr() which calculates maddr from
		 * paddr using mpa value of current->domain.
		 */
		struct domain *save;
		save = current->domain;
		current->domain = arg->domain;
		if (xencomm_copy_to_guest((void*)arg->target,
		                          sal_record, arg->ret, 0)) {
			printk("SAL_GET_STATE_INFO can't copy to user!!!!\n");
			arg->status = IA64_SAL_NO_INFORMATION_AVAILABLE;
			arg->ret = 0;
		}
	  	/* Restore current->domain to saved value. */
		current->domain = save;
	}
	spin_unlock_irqrestore(&sal_record_lock, flags);
}

void clear_state_info_on(void *data) {
	struct smp_call_args_t *arg = data;

	arg->ret = ia64_sal_clear_state_info(arg->type);
	IA64_SAL_DEBUG("SAL_CLEAR_STATE_INFO(%s) on CPU#%d returns %ld.\n",
	               rec_name[arg->type], smp_processor_id(), arg->ret);

}
  
struct sal_ret_values
sal_emulator (long index, unsigned long in1, unsigned long in2,
	      unsigned long in3, unsigned long in4, unsigned long in5,
	      unsigned long in6, unsigned long in7)
{
	struct ia64_sal_retval ret_stuff;
	unsigned long r9  = 0;
	unsigned long r10 = 0;
	long r11 = 0;
	long status;

	debugger_event(XEN_IA64_DEBUG_ON_SAL);

	status = 0;
	switch (index) {
	    case SAL_FREQ_BASE:
		if (likely(!running_on_sim))
			status = ia64_sal_freq_base(in1,&r9,&r10);
		else switch (in1) {
		      case SAL_FREQ_BASE_PLATFORM:
			r9 = 200000000;
			break;

		      case SAL_FREQ_BASE_INTERVAL_TIMER:
			r9 = 700000000;
			break;

		      case SAL_FREQ_BASE_REALTIME_CLOCK:
			r9 = 1;
			break;

		      default:
			status = -1;
			break;
		}
		break;
	    case SAL_PCI_CONFIG_READ:
		if (current->domain == dom0) {
			u64 value;
			// note that args 2&3 are swapped!!
			status = ia64_sal_pci_config_read(in1,in3,in2,&value);
			r9 = value;
		}
		else
		     printk("NON-PRIV DOMAIN CALLED SAL_PCI_CONFIG_READ\n");
		break;
	    case SAL_PCI_CONFIG_WRITE:
		if (current->domain == dom0) {
			if (((in1 & ~0xffffffffUL) && (in4 == 0)) ||
			    (in4 > 1) ||
			    (in2 > 8) || (in2 & (in2-1)))
				printk("*** SAL_PCI_CONF_WRITE?!?(adr=0x%lx,typ=0x%lx,sz=0x%lx,val=0x%lx)\n",
					in1,in4,in2,in3);
			// note that args are in a different order!!
			status = ia64_sal_pci_config_write(in1,in4,in2,in3);
		}
		else
		     printk("NON-PRIV DOMAIN CALLED SAL_PCI_CONFIG_WRITE\n");
		break;
	    case SAL_SET_VECTORS:
 		if (in1 == SAL_VECTOR_OS_BOOT_RENDEZ) {
 			if (in4 != 0 || in5 != 0 || in6 != 0 || in7 != 0) {
 				/* Sanity check: cs_length1 must be 0,
 				   second vector is reserved.  */
 				status = -2;
 			}
 			else {
				struct domain *d = current->domain;
				d->arch.sal_data->boot_rdv_ip = in2;
				d->arch.sal_data->boot_rdv_r1 = in3;
			}
 		}
 		else if (current->domain == dom0) {
			if (in1 >
			    sizeof(sal_vectors)/sizeof(sal_vectors[0])-1) {
				gdprintk(XENLOG_DEBUG,
					 "SAL_SET_VECTORS invalid in1 %ld\n",
					 in1);
				status = -2;
				break;
			}
			sal_vectors[in1].vector_type	= in1;
			sal_vectors[in1].handler_addr1	= in2;
			sal_vectors[in1].gp1		= in3;
			sal_vectors[in1].handler_len1	= in4;
			sal_vectors[in1].handler_addr2	= in5;
			sal_vectors[in1].gp2		= in6;
			sal_vectors[in1].handler_len2	= in7;
		} else {
			gdprintk(XENLOG_DEBUG, "NON-PRIV DOMAIN CALLED "
				 "SAL_SET_VECTORS %ld\n", in1);
			/*
			 * status = -2;
			 * Temporal work around untill gfw support:
			 * windows 2003 sp2/sp1 dislike -2 to crash.
			 */
			status = 0; 
		}
		break;
	    case SAL_GET_STATE_INFO:
		if (current->domain == dom0) {
			sal_queue_entry_t *e;
			unsigned long flags;
			struct smp_call_args_t arg;

			spin_lock_irqsave(&sal_queue_lock, flags);
			if (!sal_queue || list_empty(&sal_queue[in1])) {
				sal_log_record_header_t header;
				XEN_GUEST_HANDLE(void) handle =
					*(XEN_GUEST_HANDLE(void)*)&in3;

				IA64_SAL_DEBUG("SAL_GET_STATE_INFO(%s) "
				               "no sal_queue entry found.\n",
				               rec_name[in1]);
				memset(&header, 0, sizeof(header));

				if (copy_to_guest(handle, &header, 1)) {
					printk("sal_emulator: "
					       "SAL_GET_STATE_INFO can't copy "
					       "empty header to user: 0x%lx\n",
					       in3);
				}
				status = IA64_SAL_NO_INFORMATION_AVAILABLE;
				r9 = 0;
				spin_unlock_irqrestore(&sal_queue_lock, flags);
				break;
			}
			e = list_entry(sal_queue[in1].next,
			               sal_queue_entry_t, list);

			list_del(&e->list);
			spin_unlock_irqrestore(&sal_queue_lock, flags);

			IA64_SAL_DEBUG("SAL_GET_STATE_INFO(%s <= %s) "
			               "on CPU#%d.\n",
			               rec_name[e->sal_info_type],
			               rec_name[in1], e->cpuid);

			arg.type = e->sal_info_type;
			arg.target = in3;
			arg.corrected = !!((in1 != e->sal_info_type) && 
			                (e->sal_info_type == SAL_INFO_TYPE_MCA));
			arg.domain = current->domain;
			arg.status = 0;

			if (e->cpuid == smp_processor_id()) {
				IA64_SAL_DEBUG("SAL_GET_STATE_INFO: local\n");
				get_state_info_on(&arg);
			} else {
				int ret;
				IA64_SAL_DEBUG("SAL_GET_STATE_INFO: remote\n");
				ret = smp_call_function_single(e->cpuid,
				                               get_state_info_on,
				                               &arg, 0, 1);
				if (ret < 0) {
					printk("SAL_GET_STATE_INFO "
					       "smp_call_function_single error:"
					       " %d\n", ret);
					arg.ret = 0;
					arg.status =
					     IA64_SAL_NO_INFORMATION_AVAILABLE;
				}
			}
			r9 = arg.ret;
			status = arg.status;
			if (r9 == 0) {
				xfree(e);
			} else {
				/* Re-add the entry to sal_queue */
				spin_lock_irqsave(&sal_queue_lock, flags);
				list_add(&e->list, &sal_queue[in1]);
				spin_unlock_irqrestore(&sal_queue_lock, flags);
			}
		} else {
			status = IA64_SAL_NO_INFORMATION_AVAILABLE;
			r9 = 0;
		}
		break;
	    case SAL_GET_STATE_INFO_SIZE:
		r9 = ia64_sal_get_state_info_size(in1);
		break;
	    case SAL_CLEAR_STATE_INFO:
		if (current->domain == dom0) {
			sal_queue_entry_t *e;
			unsigned long flags;
			struct smp_call_args_t arg;

			spin_lock_irqsave(&sal_queue_lock, flags);
			if (list_empty(&sal_queue[in1])) {
				IA64_SAL_DEBUG("SAL_CLEAR_STATE_INFO(%s) "
				               "no sal_queue entry found.\n",
				               rec_name[in1]);
				status = IA64_SAL_NO_INFORMATION_AVAILABLE;
				r9 = 0;
				spin_unlock_irqrestore(&sal_queue_lock, flags);
				break;
			}
			e = list_entry(sal_queue[in1].next,
			               sal_queue_entry_t, list);

			list_del(&e->list);
			spin_unlock_irqrestore(&sal_queue_lock, flags);

			IA64_SAL_DEBUG("SAL_CLEAR_STATE_INFO(%s <= %s) "
			               "on CPU#%d.\n",
			               rec_name[e->sal_info_type],
			               rec_name[in1], e->cpuid);

			arg.type = e->sal_info_type;
			arg.status = 0;

			if (e->cpuid == smp_processor_id()) {
				IA64_SAL_DEBUG("SAL_CLEAR_STATE_INFO: local\n");
				clear_state_info_on(&arg);
			} else {
				int ret;
				IA64_SAL_DEBUG("SAL_CLEAR_STATE_INFO: remote\n");
				ret = smp_call_function_single(e->cpuid,
					clear_state_info_on, &arg, 0, 1);
				if (ret < 0) {
					printk("sal_emulator: "
					       "SAL_CLEAR_STATE_INFO "
					       "smp_call_function_single error:"
					       " %d\n", ret);
					arg.ret = 0;
					arg.status =
					     IA64_SAL_NO_INFORMATION_AVAILABLE;
				}
			}
			r9 = arg.ret;
			status = arg.status;
			xfree(e);
		}
		break;
	    case SAL_MC_RENDEZ:
		printk("*** CALLED SAL_MC_RENDEZ.  IGNORED...\n");
		break;
	    case SAL_MC_SET_PARAMS:
		if (current->domain == dom0) {
			if (in1 > 
			    sizeof(sal_mc_params) / sizeof(sal_mc_params[0])) {
				gdprintk(XENLOG_DEBUG,
					 "SAL_MC_SET_PARAMS invalid in1 %ld\n",
					 in1);
				status = -2;
				break;
			}
			sal_mc_params[in1].param_type	= in1;
			sal_mc_params[in1].i_or_m	= in2;
			sal_mc_params[in1].i_or_m_val	= in3;
			sal_mc_params[in1].timeout	= in4;
			sal_mc_params[in1].rz_always	= in5;
		} else {
			gdprintk(XENLOG_DEBUG,
				 "*** CALLED SAL_MC_SET_PARAMS. IGNORED...\n");
			/*
			 * status = -1;
			 * Temporal work around untill gfw support:
			 * windows 2003 sp2/sp1 dislike -1(not implemented)
			 * to crash.
			 */
			status = 0;
		}
		break;
	    case SAL_CACHE_FLUSH:
		if (1) {
			/*  Flush using SAL.
			    This method is faster but has a side effect on
			    other vcpu running on this cpu.  */
			status = ia64_sal_cache_flush (in1);
		}
		else {
			/*  Flush with fc all the domain.
			    This method is slower but has no side effects.  */
			domain_cache_flush (current->domain, in1 == 4 ? 1 : 0);
			status = 0;
		}
		break;
	    case SAL_CACHE_INIT:
		printk("*** CALLED SAL_CACHE_INIT.  IGNORED...\n");
		break;
	    case SAL_UPDATE_PAL:
		printk("*** CALLED SAL_UPDATE_PAL.  IGNORED...\n");
		break;
	    case SAL_PHYSICAL_ID_INFO:
		status = -1;
		break;
	    case SAL_XEN_SAL_RETURN:
	        if (!test_and_set_bit(_VPF_down, &current->pause_flags))
			vcpu_sleep_nosync(current);
		break;
	    case SN_SAL_GET_MASTER_NASID:
		status = -1;
		if (current->domain == dom0) {
			/* printk("*** Emulating SN_SAL_GET_MASTER_NASID ***\n"); */
			SAL_CALL_NOLOCK(ret_stuff, SN_SAL_GET_MASTER_NASID,
					0, 0, 0, 0, 0, 0, 0);
			status = ret_stuff.status;
			r9 = ret_stuff.v0;
			r10 = ret_stuff.v1;
			r11 = ret_stuff.v2;
		}
		break;
	    case SN_SAL_GET_KLCONFIG_ADDR:
		status = -1;
		if (current->domain == dom0) {
			/* printk("*** Emulating SN_SAL_GET_KLCONFIG_ADDR ***\n"); */
			SAL_CALL_NOLOCK(ret_stuff, SN_SAL_GET_KLCONFIG_ADDR,
					in1, 0, 0, 0, 0, 0, 0);
			status = ret_stuff.status;
			r9 = ret_stuff.v0;
			r10 = ret_stuff.v1;
			r11 = ret_stuff.v2;
		}
		break;
	    case SN_SAL_GET_SAPIC_INFO:
		status = -1;
		if (current->domain == dom0) {
			/* printk("*** Emulating SN_SAL_GET_SAPIC_INFO ***\n"); */
			SAL_CALL_NOLOCK(ret_stuff, SN_SAL_GET_SAPIC_INFO,
					in1, 0, 0, 0, 0, 0, 0);
			status = ret_stuff.status;
			r9 = ret_stuff.v0;
			r10 = ret_stuff.v1;
			r11 = ret_stuff.v2;
		}
		break;
	    case SN_SAL_GET_SN_INFO:
		status = -1;
		if (current->domain == dom0) {
			/* printk("*** Emulating SN_SAL_GET_SN_INFO ***\n"); */
			SAL_CALL_NOLOCK(ret_stuff, SN_SAL_GET_SN_INFO,
					in1, 0, 0, 0, 0, 0, 0);
			status = ret_stuff.status;
			r9 = ret_stuff.v0;
			r10 = ret_stuff.v1;
			r11 = ret_stuff.v2;
		}
		break;
	    case SN_SAL_IOIF_GET_HUBDEV_INFO:
		status = -1;
		if (current->domain == dom0) {
			/* printk("*** Emulating SN_SAL_IOIF_GET_HUBDEV_INFO ***\n"); */
			SAL_CALL_NOLOCK(ret_stuff, SN_SAL_IOIF_GET_HUBDEV_INFO,
					in1, in2, 0, 0, 0, 0, 0);
			status = ret_stuff.status;
			r9 = ret_stuff.v0;
			r10 = ret_stuff.v1;
			r11 = ret_stuff.v2;
		}
		break;
	    case SN_SAL_IOIF_INIT:
		status = -1;
		if (current->domain == dom0) {
			/* printk("*** Emulating SN_SAL_IOIF_INIT ***\n"); */
			SAL_CALL_NOLOCK(ret_stuff, SN_SAL_IOIF_INIT,
					0, 0, 0, 0, 0, 0, 0);
			status = ret_stuff.status;
			r9 = ret_stuff.v0;
			r10 = ret_stuff.v1;
			r11 = ret_stuff.v2;
		}
		break;
	    case SN_SAL_GET_PROM_FEATURE_SET:
		status = -1;
		if (current->domain == dom0) {
			/* printk("*** Emulating SN_SAL_GET_PROM_FEATURE_SET ***\n"); */
			SAL_CALL_NOLOCK(ret_stuff, SN_SAL_GET_PROM_FEATURE_SET,
					in1, 0, 0, 0, 0, 0, 0);
			status = ret_stuff.status;
			r9 = ret_stuff.v0;
			r10 = ret_stuff.v1;
			r11 = ret_stuff.v2;
		}
		break;
	    case SN_SAL_SET_OS_FEATURE_SET:
		status = -1;
		if (current->domain == dom0) {
			/* printk("*** Emulating SN_SAL_SET_OS_FEATURE_SET ***\n"); */
			SAL_CALL_NOLOCK(ret_stuff, SN_SAL_SET_OS_FEATURE_SET,
					in1, 0, 0, 0, 0, 0, 0);
			status = ret_stuff.status;
			r9 = ret_stuff.v0;
			r10 = ret_stuff.v1;
			r11 = ret_stuff.v2;
		}
		break;
	    case SN_SAL_SET_ERROR_HANDLING_FEATURES:
		status = -1;
		if (current->domain == dom0) {
			/* printk("*** Emulating SN_SAL_SET_ERROR_HANDLING_FEATURES ***\n"); */
			SAL_CALL_NOLOCK(ret_stuff,
					SN_SAL_SET_ERROR_HANDLING_FEATURES,
					in1, 0, 0, 0, 0, 0, 0);
			status = ret_stuff.status;
			r9 = ret_stuff.v0;
			r10 = ret_stuff.v1;
			r11 = ret_stuff.v2;
		}
		break;
#if 0
/*
 * Somehow ACPI breaks if allowing this one
 */
	    case SN_SAL_SET_CPU_NUMBER:
		status = -1;
		if (current->domain == dom0) {
			printk("*** Emulating SN_SAL_SET_CPU_NUMBER ***\n");
			SAL_CALL_NOLOCK(ret_stuff, SN_SAL_SET_CPU_NUMBER,
					in1, 0, 0, 0, 0, 0, 0);
			status = ret_stuff.status;
			r9 = ret_stuff.v0;
			r10 = ret_stuff.v1;
			r11 = ret_stuff.v2;
		}
		break;
#endif
	    case SN_SAL_LOG_CE:
		status = -1;
		if (current->domain == dom0) {
			static int log_ce = 0;
			if (!log_ce) {
				printk("*** Emulating SN_SAL_LOG_CE *** "
				       " this will only be printed once\n");
				log_ce = 1;
			}
			SAL_CALL_NOLOCK(ret_stuff, SN_SAL_LOG_CE,
					0, 0, 0, 0, 0, 0, 0);
			status = ret_stuff.status;
			r9 = ret_stuff.v0;
			r10 = ret_stuff.v1;
			r11 = ret_stuff.v2;
		}
		break;
	    case SN_SAL_IOIF_GET_DEVICE_DMAFLUSH_LIST:
		status = -1;
		if (current->domain == dom0) {
			struct sn_flush_device_common flush;
			int flush_size;

			flush_size = sizeof(struct sn_flush_device_common);
			memset(&flush, 0, flush_size);
			SAL_CALL_NOLOCK(ret_stuff,
					SN_SAL_IOIF_GET_DEVICE_DMAFLUSH_LIST,
					in1, in2, in3, &flush, 0, 0, 0);
#if 0
			printk("*** Emulating "
			       "SN_SAL_IOIF_GET_DEVICE_DMAFLUSH_LIST ***\n");
#endif
			if (ret_stuff.status == SALRET_OK) {
				XEN_GUEST_HANDLE(void) handle =
					*(XEN_GUEST_HANDLE(void)*)&in4;
				if (copy_to_guest(handle, &flush, 1)) {
					printk("SN_SAL_IOIF_GET_DEVICE_"
					       "DMAFLUSH_LIST can't copy "
					       "to user!\n");
					ret_stuff.status = SALRET_ERROR;
				}
			}

			status = ret_stuff.status;
			r9 = ret_stuff.v0;
			r10 = ret_stuff.v1;
			r11 = ret_stuff.v2;
		}
		break;
	    default:
		printk("*** CALLED SAL_ WITH UNKNOWN INDEX (%lx).  "
		       "IGNORED...\n", index);
		status = -1;
		break;
	}
	return ((struct sal_ret_values) {status, r9, r10, r11});
}

static int
safe_copy_to_guest(unsigned long to, void *from, long size)
{
	BUG_ON((unsigned)size > PAGE_SIZE);

	if (VMX_DOMAIN(current)) {
		if (is_virtual_mode(current)) {
			thash_data_t *data;
			unsigned long gpa, poff;

			/* The caller must provide a DTR or DTC mapping */
			data = vtlb_lookup(current, to, DSIDE_TLB);
			if (data) {
				gpa = data->page_flags & _PAGE_PPN_MASK;
			} else {
				data = vhpt_lookup(to);
				if (!data)
					return -1;
				gpa = __mpa_to_gpa(
					data->page_flags & _PAGE_PPN_MASK);
				gpa &= _PAGE_PPN_MASK;
			}
			poff = POFFSET(to, data->ps);
			if (poff + size > PSIZE(data->ps))
				return -1;
			to = PAGEALIGN(gpa, data->ps) | poff;
		}
		to |= XENCOMM_INLINE_FLAG;
		if (xencomm_copy_to_guest((void *)to, from, size, 0) != 0)
			return -1;
		return 0;
	} else {
		/* check for vulnerability */
		if (IS_VMM_ADDRESS(to) || IS_VMM_ADDRESS(to + size - 1))
			panic_domain(NULL, "copy to bad address:0x%lx\n", to);
		return copy_to_user((void __user *)to, from, size);
	}
}

cpumask_t cpu_cache_coherent_map;

struct cache_flush_args {
	u64 cache_type;
	u64 operation;
	u64 progress;
	long status;
};

static void
remote_pal_cache_flush(void *v)
{
	struct cache_flush_args *args = v;
	long status;
	u64 progress = args->progress;

	status = ia64_pal_cache_flush(args->cache_type, args->operation,
				      &progress, NULL);
	if (status != 0)
		args->status = status;
}

static void
remote_pal_prefetch_visibility(void *v)
{
	s64 trans_type = (s64)v;
	ia64_pal_prefetch_visibility(trans_type);
}

static void
remote_pal_mc_drain(void *v)
{
	ia64_pal_mc_drain();
}

struct ia64_pal_retval
xen_pal_emulator(unsigned long index, u64 in1, u64 in2, u64 in3)
{
	unsigned long r9  = 0;
	unsigned long r10 = 0;
	unsigned long r11 = 0;
	long status = PAL_STATUS_UNIMPLEMENTED;
	unsigned long flags;
	int processor;

	if (unlikely(running_on_sim))
		return pal_emulator_static(index);

	debugger_event(XEN_IA64_DEBUG_ON_PAL);

	// pal code must be mapped by a TR when pal is called, however
	// calls are rare enough that we will map it lazily rather than
	// at every context switch
	//efi_map_pal_code();
	switch (index) {
	    case PAL_MEM_ATTRIB:
		status = ia64_pal_mem_attrib(&r9);
		break;
	    case PAL_FREQ_BASE:
		status = ia64_pal_freq_base(&r9);
		if (status == PAL_STATUS_UNIMPLEMENTED) {
			status = ia64_sal_freq_base(0, &r9, &r10);
			r10 = 0;
		}
		break;
	    case PAL_PROC_GET_FEATURES:
		status = ia64_pal_proc_get_features(&r9,&r10,&r11);
		break;
	    case PAL_BUS_GET_FEATURES:
		status = ia64_pal_bus_get_features(
				(pal_bus_features_u_t *) &r9,
				(pal_bus_features_u_t *) &r10,
				(pal_bus_features_u_t *) &r11);
		break;
	    case PAL_FREQ_RATIOS:
		status = ia64_pal_freq_ratios(
				(struct pal_freq_ratio *) &r9,
				(struct pal_freq_ratio *) &r10,
				(struct pal_freq_ratio *) &r11);
		break;
	    case PAL_PTCE_INFO:
		/*
		 * return hard-coded xen-specific values because ptc.e
		 * is emulated on xen to always flush everything
		 * these values result in only one ptc.e instruction
		 */
		status = PAL_STATUS_SUCCESS;
		r10 = (1L << 32) | 1L;
		break;
	    case PAL_VERSION:
		status = ia64_pal_version(
				(pal_version_u_t *) &r9,
				(pal_version_u_t *) &r10);
		break;
	    case PAL_VM_PAGE_SIZE:
		status = ia64_pal_vm_page_size(&r9,&r10);
		break;
	    case PAL_DEBUG_INFO:
		status = ia64_pal_debug_info(&r9,&r10);
		break;
	    case PAL_CACHE_SUMMARY:
		status = ia64_pal_cache_summary(&r9,&r10);
		break;
	    case PAL_VM_SUMMARY:
		if (VMX_DOMAIN(current)) {
			pal_vm_info_1_u_t v1;
			pal_vm_info_2_u_t v2;
			status = ia64_pal_vm_summary((pal_vm_info_1_u_t *)&v1,
			                             (pal_vm_info_2_u_t *)&v2);
			v1.pal_vm_info_1_s.max_itr_entry = NITRS - 1;
			v1.pal_vm_info_1_s.max_dtr_entry = NDTRS - 1;
			v2.pal_vm_info_2_s.impl_va_msb -= 1;
			v2.pal_vm_info_2_s.rid_size =
				current->domain->arch.rid_bits;
			r9 = v1.pvi1_val;
			r10 = v2.pvi2_val;
		} else {
			/* Use xen-specific values.
			   hash_tag_id is somewhat random! */
			static const pal_vm_info_1_u_t v1 =
				{.pal_vm_info_1_s =
				 { .vw = 1,
				   .phys_add_size = 44,
				   .key_size = 16,
				   .max_pkr = XEN_IA64_NPKRS,
				   .hash_tag_id = 0x30,
				   .max_dtr_entry = NDTRS - 1,
				   .max_itr_entry = NITRS - 1,
				   .max_unique_tcs = 3,
				   .num_tc_levels = 2
				 }};
			pal_vm_info_2_u_t v2;
			v2.pvi2_val = 0;
			v2.pal_vm_info_2_s.rid_size =
				current->domain->arch.rid_bits;
			v2.pal_vm_info_2_s.impl_va_msb = 50;
			r9 = v1.pvi1_val;
			r10 = v2.pvi2_val;
			status = PAL_STATUS_SUCCESS;
		}
		break;
	    case PAL_VM_INFO:
		if (VMX_DOMAIN(current)) {
			status = ia64_pal_vm_info(in1, in2, 
			                          (pal_tc_info_u_t *)&r9, &r10);
			break;
		}
		if (in1 == 0 && in2 == 2) {
			/* Level 1: VHPT  */
			const pal_tc_info_u_t v =
				{ .pal_tc_info_s = {.num_sets = 128,
						    .associativity = 1,
						    .num_entries = 128,
						    .pf = 1,
						    .unified = 1,
						    .reduce_tr = 0,
						    .reserved = 0}};
			r9 = v.pti_val;
			/* Only support PAGE_SIZE tc.  */
			r10 = PAGE_SIZE;
			status = PAL_STATUS_SUCCESS;
		}
	        else if (in1 == 1 && (in2 == 1 || in2 == 2)) {
			/* Level 2: itlb/dtlb, 1 entry.  */
			const pal_tc_info_u_t v =
				{ .pal_tc_info_s = {.num_sets = 1,
						    .associativity = 1,
						    .num_entries = 1,
						    .pf = 1,
						    .unified = 0,
						    .reduce_tr = 0,
						    .reserved = 0}};
			r9 = v.pti_val;
			/* Only support PAGE_SIZE tc.  */
			r10 = PAGE_SIZE;
			status = PAL_STATUS_SUCCESS;
		} else
			status = PAL_STATUS_EINVAL;
		break;
	    case PAL_RSE_INFO:
		status = ia64_pal_rse_info(&r9, (pal_hints_u_t *)&r10);
		break;
	    case PAL_REGISTER_INFO:
		status = ia64_pal_register_info(in1, &r9, &r10);
		break;
	    case PAL_CACHE_FLUSH:
		if (in3 != 0) /* Initially progress_indicator must be 0 */
			panic_domain(NULL, "PAL_CACHE_FLUSH ERROR, "
				     "progress_indicator=%lx", in3);

		/* Always call Host Pal in int=0 */
		in2 &= ~PAL_CACHE_FLUSH_CHK_INTRS;

		if (in1 != PAL_CACHE_TYPE_COHERENT) {
			struct cache_flush_args args = {
				.cache_type = in1,
				.operation = in2,
				.progress = 0,
				.status = 0
			};
			smp_call_function(remote_pal_cache_flush,
					  (void *)&args, 1, 1);
			if (args.status != 0)
				panic_domain(NULL, "PAL_CACHE_FLUSH ERROR, "
					     "remote status %lx", args.status);
		}

		/*
		 * Call Host PAL cache flush
		 * Clear psr.ic when call PAL_CACHE_FLUSH
		 */
		r10 = in3;
		local_irq_save(flags);
		processor = current->processor;
		status = ia64_pal_cache_flush(in1, in2, &r10, &r9);
		local_irq_restore(flags);

		if (status != 0)
			panic_domain(NULL, "PAL_CACHE_FLUSH ERROR, "
			             "status %lx", status);

		if (in1 == PAL_CACHE_TYPE_COHERENT) {
			cpus_setall(current->arch.cache_coherent_map);
			cpu_clear(processor, current->arch.cache_coherent_map);
			cpus_setall(cpu_cache_coherent_map);
			cpu_clear(processor, cpu_cache_coherent_map);
		}
		break;
	    case PAL_PERF_MON_INFO:
		{
			unsigned long pm_buffer[16];
			status = ia64_pal_perf_mon_info(
					pm_buffer,
					(pal_perf_mon_info_u_t *) &r9);
			if (status != 0) {
				printk("PAL_PERF_MON_INFO fails ret=%ld\n", status);
				break;
			}
			if (safe_copy_to_guest(
				in1, pm_buffer, sizeof(pm_buffer))) {
				status = PAL_STATUS_EINVAL;
				goto fail_to_copy;
			}
		}
		break;
	    case PAL_CACHE_INFO:
		{
			pal_cache_config_info_t ci;
			status = ia64_pal_cache_config_info(in1,in2,&ci);
			if (status != 0)
				break;
			r9 = ci.pcci_info_1.pcci1_data;
			r10 = ci.pcci_info_2.pcci2_data;
		}
		break;
	    case PAL_VM_TR_READ:	/* FIXME: vcpu_get_tr?? */
		printk("%s: PAL_VM_TR_READ unimplmented, ignored\n", __func__);
		break;
	    case PAL_HALT_INFO:
	        {
		    /* 1000 cycles to enter/leave low power state,
		       consumes 10 mW, implemented and cache/TLB coherent.  */
		    unsigned long res = 1000UL | (1000UL << 16) | (10UL << 32)
			    | (1UL << 61) | (1UL << 60);
		    if (safe_copy_to_guest (in1, &res, sizeof (res))) {
			    status = PAL_STATUS_EINVAL;    
			    goto fail_to_copy;
		    }
		    status = PAL_STATUS_SUCCESS;
	        }
		break;
	    case PAL_HALT:
		set_bit(_VPF_down, &current->pause_flags);
		vcpu_sleep_nosync(current);
		status = PAL_STATUS_SUCCESS;
		break;
	    case PAL_HALT_LIGHT:
		if (VMX_DOMAIN(current)) {
			/* Called by VTI.  */
			if (!is_unmasked_irq(current)) {
				do_sched_op_compat(SCHEDOP_block, 0);
				do_softirq();
			}
			status = PAL_STATUS_SUCCESS;
		}
		break;
	    case PAL_PLATFORM_ADDR:
		if (VMX_DOMAIN(current))
			status = PAL_STATUS_SUCCESS;
		break;
	    case PAL_FIXED_ADDR:
		status = PAL_STATUS_SUCCESS;
		r9 = current->vcpu_id;
		break;
	    case PAL_PREFETCH_VISIBILITY:
		status = ia64_pal_prefetch_visibility(in1);
		if (status == 0) {
			/* must be performed on all remote processors 
			   in the coherence domain. */
			smp_call_function(remote_pal_prefetch_visibility,
					  (void *)in1, 1, 1);
			status = 1; /* no more necessary on remote processor */
		}
		break;
	    case PAL_MC_DRAIN:
		status = ia64_pal_mc_drain();
		/* FIXME: All vcpus likely call PAL_MC_DRAIN.
		   That causes the congestion. */
		smp_call_function(remote_pal_mc_drain, NULL, 1, 1);
		break;
	    case PAL_BRAND_INFO:
		if (in1 == 0) {
			char brand_info[128];
			status = ia64_pal_get_brand_info(brand_info);
			if (status != PAL_STATUS_SUCCESS)
				break;
			if (safe_copy_to_guest(in2, brand_info,
					       sizeof(brand_info))) {
				status = PAL_STATUS_EINVAL;
				goto fail_to_copy;
			}
		} else {
			status = PAL_STATUS_EINVAL;
		}
		break;
	    case PAL_LOGICAL_TO_PHYSICAL:
	    case PAL_GET_PSTATE:
	    case PAL_CACHE_SHARED_INFO:
		/* Optional, no need to complain about being unimplemented */
		break;
	    default:
		printk("%s: Unimplemented PAL Call %lu\n", __func__, index);
		break;
	}
	return ((struct ia64_pal_retval) {status, r9, r10, r11});

fail_to_copy:
	gdprintk(XENLOG_WARNING,
		"PAL(%ld) fail to copy!!! args 0x%lx 0x%lx 0x%lx\n",
		index, in1, in2, in3);
	return ((struct ia64_pal_retval) {status, r9, r10, r11});
}

// given a current domain (virtual or metaphysical) address, return the virtual address
static unsigned long
efi_translate_domain_addr(unsigned long domain_addr, IA64FAULT *fault,
			  struct page_info** page)
{
	struct vcpu *v = current;
	unsigned long mpaddr = domain_addr;
	unsigned long virt;
	*fault = IA64_NO_FAULT;

again:
 	if (v->domain->arch.sal_data->efi_virt_mode) {
		*fault = vcpu_tpa(v, domain_addr, &mpaddr);
		if (*fault != IA64_NO_FAULT) return 0;
	}

	virt = (unsigned long)domain_mpa_to_imva(v->domain, mpaddr);
	*page = virt_to_page(virt);
	if (get_page(*page, current->domain) == 0) {
		if (page_get_owner(*page) != current->domain) {
			// which code is appropriate?
			*fault = IA64_FAULT;
			return 0;
		}
		goto again;
	}

	return virt;
}

static efi_status_t
efi_emulate_get_time(
	unsigned long tv_addr, unsigned long tc_addr,
	IA64FAULT *fault)
{
	unsigned long tv, tc = 0;
	struct page_info *tv_page = NULL;
	struct page_info *tc_page = NULL;
	efi_status_t status = 0;
	efi_time_t *tvp;
	struct tm timeptr;
	unsigned long xtimesec;

	tv = efi_translate_domain_addr(tv_addr, fault, &tv_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;
	if (tc_addr) {
		tc = efi_translate_domain_addr(tc_addr, fault, &tc_page);
		if (*fault != IA64_NO_FAULT)
			goto errout;
	}

	spin_lock(&efi_time_services_lock);
	status = (*efi.get_time)((efi_time_t *) tv, (efi_time_cap_t *) tc);
	tvp = (efi_time_t *)tv;
	xtimesec = mktime(tvp->year, tvp->month, tvp->day, tvp->hour,
	                  tvp->minute, tvp->second);
	xtimesec += current->domain->time_offset_seconds;
	timeptr = gmtime(xtimesec);
	tvp->second = timeptr.tm_sec;
	tvp->minute = timeptr.tm_min;
	tvp->hour   = timeptr.tm_hour;
	tvp->day    = timeptr.tm_mday;
	tvp->month  = timeptr.tm_mon + 1;
	tvp->year   = timeptr.tm_year + 1900;
	spin_unlock(&efi_time_services_lock);

errout:
	if (tc_page != NULL)
		put_page(tc_page);
	if (tv_page != NULL)
		put_page(tv_page);

	return status;
}

static efi_status_t
efi_emulate_set_time(
	unsigned long tv_addr, IA64FAULT *fault)
{
	unsigned long tv;
	struct page_info *tv_page = NULL;
	efi_status_t status = 0;

	if (current->domain != dom0)
		return EFI_UNSUPPORTED;

	tv = efi_translate_domain_addr(tv_addr, fault, &tv_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;

	spin_lock(&efi_time_services_lock);
	status = (*efi.set_time)((efi_time_t *)tv);
	spin_unlock(&efi_time_services_lock);

errout:
	if (tv_page != NULL)
		put_page(tv_page);

	return status;
}

static efi_status_t
efi_emulate_get_wakeup_time(
	unsigned long e_addr, unsigned long p_addr,
	unsigned long tv_addr, IA64FAULT *fault)
{
	unsigned long enabled, pending, tv;
	struct page_info *e_page = NULL, *p_page = NULL,
	                 *tv_page = NULL;
	efi_status_t status = 0;

	if (current->domain != dom0)
		return EFI_UNSUPPORTED;

	if (!e_addr || !p_addr || !tv_addr)
		return EFI_INVALID_PARAMETER;

	enabled = efi_translate_domain_addr(e_addr, fault, &e_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;
	pending = efi_translate_domain_addr(p_addr, fault, &p_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;
	tv = efi_translate_domain_addr(tv_addr, fault, &tv_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;

	spin_lock(&efi_time_services_lock);
	status = (*efi.get_wakeup_time)((efi_bool_t *)enabled,
	                                (efi_bool_t *)pending,
	                                (efi_time_t *)tv);
	spin_unlock(&efi_time_services_lock);

errout:
	if (e_page != NULL)
		put_page(e_page);
	if (p_page != NULL)
		put_page(p_page);
	if (tv_page != NULL)
		put_page(tv_page);

	return status;
}

static efi_status_t
efi_emulate_set_wakeup_time(
	unsigned long enabled, unsigned long tv_addr,
	IA64FAULT *fault)
{
	unsigned long tv = 0;
	struct page_info *tv_page = NULL;
	efi_status_t status = 0;

	if (current->domain != dom0)
		return EFI_UNSUPPORTED;

	if (tv_addr) {
		tv = efi_translate_domain_addr(tv_addr, fault, &tv_page);
		if (*fault != IA64_NO_FAULT)
			goto errout;
	}

	spin_lock(&efi_time_services_lock);
	status = (*efi.set_wakeup_time)((efi_bool_t)enabled,
	                                (efi_time_t *)tv);
	spin_unlock(&efi_time_services_lock);

errout:
	if (tv_page != NULL)
		put_page(tv_page);

	return status;
}

static efi_status_t
efi_emulate_get_variable(
	unsigned long name_addr, unsigned long vendor_addr,
	unsigned long attr_addr, unsigned long data_size_addr,
	unsigned long data_addr, IA64FAULT *fault)
{
	unsigned long name, vendor, attr = 0, data_size, data;
	struct page_info *name_page = NULL, *vendor_page = NULL,
	                 *attr_page = NULL, *data_size_page = NULL,
	                 *data_page = NULL;
	efi_status_t status = 0;

	if (current->domain != dom0)
		return EFI_UNSUPPORTED;

	name = efi_translate_domain_addr(name_addr, fault, &name_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;
	vendor = efi_translate_domain_addr(vendor_addr, fault, &vendor_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;
	data_size = efi_translate_domain_addr(data_size_addr, fault,
	                                      &data_size_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;
	data = efi_translate_domain_addr(data_addr, fault, &data_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;
	if (attr_addr) {
		attr = efi_translate_domain_addr(attr_addr, fault, &attr_page);
		if (*fault != IA64_NO_FAULT)
			goto errout;
	}

	status = (*efi.get_variable)((efi_char16_t *)name,
	                             (efi_guid_t *)vendor,
	                             (u32 *)attr,
	                             (unsigned long *)data_size,
	                             (void *)data);

errout:
	if (name_page != NULL)
		put_page(name_page);
	if (vendor_page != NULL)
		put_page(vendor_page);
	if (attr_page != NULL)
		put_page(attr_page);
	if (data_size_page != NULL)
		put_page(data_size_page);
	if (data_page != NULL)
		put_page(data_page);

	return status;
}

static efi_status_t
efi_emulate_get_next_variable(
	unsigned long name_size_addr, unsigned long name_addr,
	unsigned long vendor_addr, IA64FAULT *fault)
{
	unsigned long name_size, name, vendor;
	struct page_info *name_size_page = NULL, *name_page = NULL,
	                 *vendor_page = NULL;
	efi_status_t status = 0;

	if (current->domain != dom0)
		return EFI_UNSUPPORTED;

	name_size = efi_translate_domain_addr(name_size_addr, fault,
	                                      &name_size_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;
	name = efi_translate_domain_addr(name_addr, fault, &name_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;
	vendor = efi_translate_domain_addr(vendor_addr, fault, &vendor_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;

	status = (*efi.get_next_variable)((unsigned long *)name_size,
	                                  (efi_char16_t *)name,
	                                  (efi_guid_t *)vendor);

errout:
	if (name_size_page != NULL)
		put_page(name_size_page);
	if (name_page != NULL)
		put_page(name_page);
	if (vendor_page != NULL)
		put_page(vendor_page);

	return status;
}

static efi_status_t
efi_emulate_set_variable(
	unsigned long name_addr, unsigned long vendor_addr, 
	unsigned long attr, unsigned long data_size, 
	unsigned long data_addr, IA64FAULT *fault)
{
	unsigned long name, vendor, data;
	struct page_info *name_page = NULL, *vendor_page = NULL,
	                 *data_page = NULL;
	efi_status_t status = 0;

	if (current->domain != dom0)
		return EFI_UNSUPPORTED;

	name = efi_translate_domain_addr(name_addr, fault, &name_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;
	vendor = efi_translate_domain_addr(vendor_addr, fault, &vendor_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;
	data = efi_translate_domain_addr(data_addr, fault, &data_page);
	if (*fault != IA64_NO_FAULT)
		goto errout;

	status = (*efi.set_variable)((efi_char16_t *)name,
	                             (efi_guid_t *)vendor,
	                             attr,
	                             data_size,
	                             (void *)data);

errout:
	if (name_page != NULL)
		put_page(name_page);
	if (vendor_page != NULL)
		put_page(vendor_page);
	if (data_page != NULL)
		put_page(data_page);

	return status;
}

static efi_status_t
efi_emulate_set_virtual_address_map(
	unsigned long memory_map_size, unsigned long descriptor_size,
	u32 descriptor_version, efi_memory_desc_t *virtual_map)
{
	void *efi_map_start, *efi_map_end, *p;
	efi_memory_desc_t entry, *md = &entry;
	u64 efi_desc_size;

	unsigned long *vfn;
	struct domain *d = current->domain;
	efi_runtime_services_t *efi_runtime = d->arch.efi_runtime;
	fpswa_interface_t *fpswa_inf = d->arch.fpswa_inf;

	if (descriptor_version != EFI_MEMDESC_VERSION) {
		printk ("efi_emulate_set_virtual_address_map: memory "
		        "descriptor version unmatched (%d vs %d)\n",
		        (int)descriptor_version, EFI_MEMDESC_VERSION);
		return EFI_INVALID_PARAMETER;
	}

	if (descriptor_size != sizeof(efi_memory_desc_t)) {
		printk ("efi_emulate_set_virtual_address_map: memory descriptor size unmatched\n");
		return EFI_INVALID_PARAMETER;
	}

	if (d->arch.sal_data->efi_virt_mode)
		return EFI_UNSUPPORTED;

	efi_map_start = virtual_map;
	efi_map_end   = efi_map_start + memory_map_size;
	efi_desc_size = sizeof(efi_memory_desc_t);

	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
		if (copy_from_user(&entry, p, sizeof(efi_memory_desc_t))) {
			printk ("efi_emulate_set_virtual_address_map: copy_from_user() fault. addr=0x%p\n", p);
			return EFI_UNSUPPORTED;
		}

		/* skip over non-PAL_CODE memory descriptors; EFI_RUNTIME is included in PAL_CODE. */
                if (md->type != EFI_PAL_CODE)
                        continue;

#define EFI_HYPERCALL_PATCH_TO_VIRT(tgt,call) \
	do { \
		vfn = (unsigned long *) domain_mpa_to_imva(d, tgt); \
		*vfn++ = FW_HYPERCALL_##call##_INDEX * 16UL + md->virt_addr; \
		*vfn++ = 0; \
	} while (0)

		EFI_HYPERCALL_PATCH_TO_VIRT(efi_runtime->get_time,EFI_GET_TIME);
		EFI_HYPERCALL_PATCH_TO_VIRT(efi_runtime->set_time,EFI_SET_TIME);
		EFI_HYPERCALL_PATCH_TO_VIRT(efi_runtime->get_wakeup_time,EFI_GET_WAKEUP_TIME);
		EFI_HYPERCALL_PATCH_TO_VIRT(efi_runtime->set_wakeup_time,EFI_SET_WAKEUP_TIME);
		EFI_HYPERCALL_PATCH_TO_VIRT(efi_runtime->set_virtual_address_map,EFI_SET_VIRTUAL_ADDRESS_MAP);
		EFI_HYPERCALL_PATCH_TO_VIRT(efi_runtime->get_variable,EFI_GET_VARIABLE);
		EFI_HYPERCALL_PATCH_TO_VIRT(efi_runtime->get_next_variable,EFI_GET_NEXT_VARIABLE);
		EFI_HYPERCALL_PATCH_TO_VIRT(efi_runtime->set_variable,EFI_SET_VARIABLE);
		EFI_HYPERCALL_PATCH_TO_VIRT(efi_runtime->get_next_high_mono_count,EFI_GET_NEXT_HIGH_MONO_COUNT);
		EFI_HYPERCALL_PATCH_TO_VIRT(efi_runtime->reset_system,EFI_RESET_SYSTEM);

		vfn = (unsigned long *) domain_mpa_to_imva(d, (unsigned long) fpswa_inf->fpswa);
		*vfn++ = FW_HYPERCALL_FPSWA_PATCH_INDEX * 16UL + md->virt_addr;
		*vfn   = 0;
		fpswa_inf->fpswa = (void *) (FW_HYPERCALL_FPSWA_ENTRY_INDEX * 16UL + md->virt_addr);
		break;
	}

	/* The virtual address map has been applied. */
	d->arch.sal_data->efi_virt_mode = 1;

	return EFI_SUCCESS;
}

efi_status_t
efi_emulator (struct pt_regs *regs, IA64FAULT *fault)
{
	struct vcpu *v = current;
	efi_status_t status;

	debugger_event(XEN_IA64_DEBUG_ON_EFI);

	*fault = IA64_NO_FAULT;

	switch (regs->r2) {
	    case FW_HYPERCALL_EFI_RESET_SYSTEM:
	        {
		    u8 reason;
		    unsigned long val = vcpu_get_gr(v,32);
		    switch (val)
		    {
		    case EFI_RESET_SHUTDOWN:
			    reason = SHUTDOWN_poweroff;
			    break;
		    case EFI_RESET_COLD:
		    case EFI_RESET_WARM:
		    default:
			    reason = SHUTDOWN_reboot;
			    break;
		    }
		    domain_shutdown (current->domain, reason);
		}
		status = EFI_UNSUPPORTED;
		break;
	    case FW_HYPERCALL_EFI_GET_TIME:
		status = efi_emulate_get_time (
				vcpu_get_gr(v,32),
				vcpu_get_gr(v,33),
				fault);
		break;
	    case FW_HYPERCALL_EFI_SET_TIME:
		status = efi_emulate_set_time (
				vcpu_get_gr(v,32),
				fault);
		break;
	    case FW_HYPERCALL_EFI_GET_WAKEUP_TIME:
		status = efi_emulate_get_wakeup_time (
				vcpu_get_gr(v,32),
				vcpu_get_gr(v,33),
				vcpu_get_gr(v,34),
				fault);
		break;
	    case FW_HYPERCALL_EFI_SET_WAKEUP_TIME:
		status = efi_emulate_set_wakeup_time (
				vcpu_get_gr(v,32),
				vcpu_get_gr(v,33),
				fault);
		break;
	    case FW_HYPERCALL_EFI_GET_VARIABLE:
		status = efi_emulate_get_variable (
				vcpu_get_gr(v,32),
				vcpu_get_gr(v,33),
				vcpu_get_gr(v,34),
				vcpu_get_gr(v,35),
				vcpu_get_gr(v,36),
				fault);
		break;
	    case FW_HYPERCALL_EFI_GET_NEXT_VARIABLE:
		status = efi_emulate_get_next_variable (
				vcpu_get_gr(v,32),
				vcpu_get_gr(v,33),
				vcpu_get_gr(v,34),
				fault);
		break;
	    case FW_HYPERCALL_EFI_SET_VARIABLE:
		status = efi_emulate_set_variable (
				vcpu_get_gr(v,32),
				vcpu_get_gr(v,33),
				vcpu_get_gr(v,34),
				vcpu_get_gr(v,35),
				vcpu_get_gr(v,36),
				fault);
		break;
	    case FW_HYPERCALL_EFI_SET_VIRTUAL_ADDRESS_MAP:
		status = efi_emulate_set_virtual_address_map (
				vcpu_get_gr(v,32),
				vcpu_get_gr(v,33),
 				(u32) vcpu_get_gr(v,34),
				(efi_memory_desc_t *) vcpu_get_gr(v,35));
		break;
	    case FW_HYPERCALL_EFI_GET_NEXT_HIGH_MONO_COUNT:
		// FIXME: need fixes in efi.h from 2.6.9
		status = EFI_UNSUPPORTED;
		break;
	    default:
		printk("unknown ia64 fw hypercall %lx\n", regs->r2);
		status = EFI_UNSUPPORTED;
	}

	return status;
}

void
do_ssc(unsigned long ssc, struct pt_regs *regs)
{
	unsigned long arg0, arg1, arg2, arg3, retval;
	char buf[2];
/**/	static int last_fd, last_count;	// FIXME FIXME FIXME
/**/					// BROKEN FOR MULTIPLE DOMAINS & SMP
/**/	struct ssc_disk_stat { int fd; unsigned count;} *stat, last_stat;