aboutsummaryrefslogtreecommitdiffstats
path: root/docs/ja/feature_advanced_keycodes.md
blob: c6e21feb9463c7a9a262d71245c82fb889077388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# レイヤーの切り替えとトグル :id=switching-and-toggling-layers

<!---
  original document: 5d5ff80:docs/feature_advanced_keycodes.md
  git diff 5d5ff80 HEAD -- docs/feature_advanced_keycodes.md | cat
-->

これらの機能により、様々な方法でレイヤーをアクティブ化することができます。レイヤーは一般的に独立したレイアウトでは無いことに注意してください -- 複数のレイヤーを一度にアクティブ化することができ、レイヤーが `KC_TRNS` を使ってキーの押下を下のレイヤーに渡すことが一般的です。レイヤーの詳細については、[キーマップの概要](ja/keymap.md#keymap-and-layers)を見てください。MO()、LM()、TT() あるいは LT() を使って一時的なレイヤーの切り替えを使う場合、上のレイヤーのキーを透過にするようにしてください。さもないと意図したように動作しないかもしれません。

* `DF(layer)` - デフォルトレイヤーを切り替えます。デフォルトレイヤーは、他のレイヤーがその上に積み重なっている、常にアクティブな基本レイヤーです。デフォルトレイヤーの詳細については以下を見てください。これは QWERTY から Dvorak レイアウトに切り替えるために使うことができます。(これは一時的な切り替えであり、キーボードの電源が切れるまでしか持続しないことに注意してください。デフォルトレイヤーを永続的に変更するには、[process_record_user](ja/custom_quantum_functions.md#programming-the-behavior-of-any-keycode) 内で `set_single_persistent_default_layer` 関数を呼び出すなど、より深いカスタマイズが必要です。)
* `MO(layer)` - 一時的に*レイヤー*をアクティブにします。キーを放すとすぐに、レイヤーは非アクティブになります。
* `LM(layer, mod)` - (`MO` のように)一時的に*レイヤー*をアクティブにしますが、モディファイア *mod* がアクティブな状態です。layer 0-15 と、左モディファイアのみをサポートします: `MOD_LCTL``MOD_LSFT``MOD_LALT``MOD_LGUI` (`KC_` の代わりに `MOD_` 定数を使うことに注意してください)。これらのモディファイアは、例えば `LM(_RAISE, MOD_LCTL | MOD_LALT)` のように、ビット単位の OR を使って組み合わせることができます。
* `LT(layer, kc)` - ホールドされた時に*レイヤー*を一時的にアクティブにし、タップされた時に *kc* を送信します。layer 0-15 のみをサポートします。
* `OSL(layer)` - 次のキーが押されるまで、一時的に*レイヤー*をアクティブにします。詳細と追加機能については、[ワンショットキー](ja/one_shot_keys.md)を見てください。
* `TG(layer)` - *レイヤー*を切り替えます。非アクティブな場合はアクティブにし、逆も同様です。
* `TO(layer)` - *レイヤー*をアクティブにし、他の全てのレイヤー(デフォルトレイヤーを除く)を非アクティブにします。この関数は特別です。1つのレイヤーをアクティブなレイヤースタックに追加/削除する代わりに、現在のアクティブなレイヤーを完全に置き換え、唯一上位のレイヤーを下位のレイヤーで置き換えることができるからです。これはキーダウンで(キーが押されるとすぐに)アクティブになります。
* `TT(layer)` - レイヤーのタップ切り替え。キーを押したままにすると*レイヤー*がアクティブにされ、放すと非アクティブになります (`MO` 風)。繰り返しタップすると、レイヤーはオンあるいはオフを切り替えます (`TG` 風)。デフォルトでは5回のタップが必要ですが、`TAPPING_TOGGLE` を定義することで変更することができます -- 例えば、2回のタップだけで切り替えるには、`#define TAPPING_TOGGLE 2` を定義します。

## 注意事項

現在のところ、`LT()` と `MT()` は[基本的なキーコードセット](ja/keycodes_basic.md)に制限されています。つまり、`LCTL()`、`KC_TILD` あるいは `0xFF` より大きなキーコードを使うことができません。レイヤータップあるいはモッドタップのキーコードの一部として指定されたモディファイアは無視されます。タップしたキーコードにモディファイアを適用する必要がある場合は、[タップダンス](ja/feature_tap_dance.md#example-5-using-tap-dance-for-advanced-mod-tap-and-layer-tap-keys)を使うことができます。

さらに、モッドタップあるいはレイヤータップで少なくとも1つの右手用のモディファイアが指定された場合、指定された全てのモディファイアが右手用になるため、2つをうまく組み合わせて一致させることはできません。

# レイヤーの使用

レイヤーを切り替える時は注意してください。(キーボードを取り外さずに)そのレイヤーを非アクティブにすることができずレイヤーから移動できなくなる可能性があります。最も一般的な問題を避けるためのガイドラインを作成しました。

## 初心者

QMK を使い始めたばかりの場合は、全てを単純にしたいでしょう。レイヤーをセットアップする時は、これらのガイドラインに従ってください:

* デフォルトの "base" レイヤーとして、layer 0 をセットアップします。これは通常の入力レイヤーであり、任意のレイアウト (qwerty、dvorak、colemak など)にすることができます。通常はキーボードのキーのほとんどまたは全てが定義されているため、これを最下位のレイヤーとして設定することが重要です。そうすることで、もしそれが他のレイヤーの上 (つまりレイヤー番号が大きい)にある場合の影響を防ぎます。
* layer 0 をルートとして、レイヤーを "ツリー" レイアウトに配置します。他の複数のレイヤーから同じレイヤーに行こうとしないでください。
* 各レイヤーのキーマップでは、より高い番号のレイヤーのみを参照します。レイヤーは最大の番号(最上位)のアクティブレイヤーから処理されるため、下位レイヤーの状態を変更するのは難しくエラーが発生しやすくなります。

## 中級ユーザ

複数の基本レイヤーが必要な場合があります。例えば、QWERTY と Dvorak を切り替える場合、国ごとに異なるレイアウトを切り替える場合、あるいは異なるビデオゲームごとにレイアウトを切り替える場合などです。基本レイヤーは常に最小の番号のレイヤーである必要があります。複数の基本レイヤーがある場合、常にそれらを相互排他的に扱う必要があります。1つの基本レイヤーがオンの場合、他をオフにします。

## 上級ユーザ

レイヤーがどのように動作し、何ができるかを理解したら、より創造的になります。初心者のセクションで列挙されている規則は、幾つかの巧妙な詳細を回避するのに役立ちますが、特に超コンパクトなキーボードのユーザにとって制約になる場合があります。レイヤーの仕組みを理解することで、レイヤーをより高度な方法で使うことができます。

レイヤーは番号順に上に積み重なっています。キーの押下の動作を決定する時に、QMK は上から順にレイヤーを走査し、`KC_TRNS` に設定されていない最初のアクティブなレイヤーに到達すると停止します。結果として、現在のレイヤーよりも数値的に低いレイヤーをアクティブにし、現在のレイヤー(あるいはアクティブでターゲットレイヤーよりも高い別のレイヤー)に `KC_TRNS` 以外のものがある場合、それが送信されるキーであり、アクティブ化したばかりのレイヤー上のキーではありません。これが、ほとんどの人の "なぜレイヤーが切り替わらないのか" 問題の原因です。

場合によっては、マクロ内あるいはタップダンスルーチンの一部としてレイヤーを切り替えほうが良いかもしれません。`layer_on` はレイヤーをアクティブにし、`layer_off` はそれを非アクティブにします。もっと多くのレイヤーに関する関数は、[action_layer.h](https://github.com/qmk/qmk_firmware/blob/master/tmk_core/common/action_layer.h) で見つけることができます。

# 修飾キー :id=modifier-keys

以下のようにキーコードとモディファイアを組み合わせることができます。押すと、モディファイアのキーダウンイベントが送信され、次に `kc` のキーダウンイベントが送信されます。放すと、`kc` のキーアップイベントが送信され、次にモディファイアのキーアップイベントが送信されます。

| キー | エイリアス | 説明 |
|----------|-------------------------------|----------------------------------------------------|
| `LCTL(kc)` | `C(kc)` | 左 Control を押しながら `kc` を押します。 |
| `LSFT(kc)` | `S(kc)` | 左 Shift を押しながら `kc` を押します。 |
| `LALT(kc)` | `A(kc)`, `LOPT(kc)` | 左 Alt を押しながら `kc`を押します。 |
| `LGUI(kc)` | `G(kc)`, `LCMD(kc)`, `LWIN(kc)` | 左 GUI を押しながら `kc` を押します。 |
| `RCTL(kc)` |  | 右 Control を押しながら `kc` を押します。 |
| `RSFT(kc)` |  | 右 Shift を押しながら `kc` を押します。 |
| `RALT(kc)` | `ROPT(kc)`, `ALGR(kc)` | 右 Alt を押しながら `kc` を押します。 |
| `RGUI(kc)` | `RCMD(kc)`, `LWIN(kc)` | 右 GUI を押しながら `kc` を押します。 |
| `SGUI(kc)` | `SCMD(kc)`, `SWIN(kc)` | 左 Shift と左 GUI を押しながら `kc` を押します。 |
| `LCA(kc)` |  | 左 Control と左 Alt を押しながら `kc` を押します。 |
| `LCAG(kc)` |  | 左 Control、左 Alt、左 GUI を押しながら `kc` を押します。 |
| `MEH(kc)` |  | 左 Control、左 Shift、左 Alt を押しながら `kc` を押します。 |
| `HYPR(kc)` |  | 左 Control、左 Shift、左 Alt、左 GUI を押しながら `kc` を押します。 |

また、それらを繋げることができます。例えば、`LCTL(LALT(KC_DEL))` は1回のキー押下で Control+Alt+Delete を送信するキーを作成します。

# 過去の内容

このページには多くの機能が含まれていました。このページを構成していた多くのセクションをそれぞれのページに移動しました。これより下は全て単なるリダイレクトであるため、web上で古いリンクをたどっている人は探しているものを見つけることができます。

## モッドタップ :id=mod-tap

* [モッドタップ](ja/mod_tap.md)

## ワンショットキー :id=one-shot-keys

* [ワンショットキー](ja/one_shot_keys.md)

## タップホールド設定オプション :id=tap-hold-configuration-options

* [タップホールド設定オプション](ja/tap_hold.md)
> 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
/******************************************************************************
 * xc_linux_build.c
 */

#include <stddef.h>
#include "xg_private.h"
#include "xc_private.h"
#include <xenctrl.h>

#include "xc_elf.h"
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <zlib.h>

/* Handy for printing out '0' prepended values at native pointer size */
#define _p(a) ((void *) ((ulong)a))

#define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
#define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
#if defined(__i386__)
#define L3_PROT (_PAGE_PRESENT)
#elif defined(__x86_64__)
#define L3_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
#define L4_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
#endif

#ifdef __ia64__
#define get_tot_pages xc_get_max_pages
#else
#define get_tot_pages xc_get_tot_pages
#endif

#define round_pgup(_p)    (((_p)+(PAGE_SIZE-1))&PAGE_MASK)
#define round_pgdown(_p)  ((_p)&PAGE_MASK)

struct initrd_info {
    enum { INITRD_none, INITRD_file, INITRD_mem } type;
    unsigned long len;
    union {
        gzFile file_handle;
        char *mem_addr;
    } u;
};

static const char *feature_names[XENFEAT_NR_SUBMAPS*32] = {
    [XENFEAT_writable_page_tables]       = "writable_page_tables",
    [XENFEAT_writable_descriptor_tables] = "writable_descriptor_tables",
    [XENFEAT_auto_translated_physmap]    = "auto_translated_physmap",
    [XENFEAT_supervisor_mode_kernel]     = "supervisor_mode_kernel",
    [XENFEAT_pae_pgdir_above_4gb]        = "pae_pgdir_above_4gb"
};

static inline void set_feature_bit (int nr, uint32_t *addr)
{
    addr[nr>>5] |= (1<<(nr&31));
}

static inline int test_feature_bit(int nr, uint32_t *addr)
{
    return !!(addr[nr>>5] & (1<<(nr&31)));
}

static int parse_features(
    const char *feats,
    uint32_t supported[XENFEAT_NR_SUBMAPS],
    uint32_t required[XENFEAT_NR_SUBMAPS])
{
    const char *end, *p;
    int i, req;

    if ( (end = strchr(feats, ',')) == NULL )
        end = feats + strlen(feats);

    while ( feats < end )
    {
        p = strchr(feats, '|');
        if ( (p == NULL) || (p > end) )
            p = end;

        req = (*feats == '!');
        if ( req )
            feats++;

        for ( i = 0; i < XENFEAT_NR_SUBMAPS*32; i++ )
        {
            if ( feature_names[i] == NULL )
                continue;

            if ( strncmp(feature_names[i], feats, p-feats) == 0 )
            {
                set_feature_bit(i, supported);
                if ( required && req )
                    set_feature_bit(i, required);
                break;
            }
        }

        if ( i == XENFEAT_NR_SUBMAPS*32 )
        {
            ERROR("Unknown feature \"%.*s\".", (int)(p-feats), feats);
            if ( req )
            {
                ERROR("Kernel requires an unknown hypervisor feature.");
                return -EINVAL;
            }
        }

        feats = p;
        if ( *feats == '|' )
            feats++;
    }

    return -EINVAL;
}

static int probeimageformat(const char *image,
                            unsigned long image_size,
                            struct load_funcs *load_funcs)
{
    if ( probe_elf(image, image_size, load_funcs) &&
         probe_bin(image, image_size, load_funcs) )
    {
        ERROR( "Unrecognized image format" );
        return -EINVAL;
    }

    return 0;
}

static int load_initrd(int xc_handle, domid_t dom,
                struct initrd_info *initrd,
                unsigned long physbase,
                xen_pfn_t *phys_to_mach)
{
    char page[PAGE_SIZE];
    unsigned long pfn_start, pfn, nr_pages;

    if ( initrd->type == INITRD_none )
        return 0;

    pfn_start = physbase >> PAGE_SHIFT;
    nr_pages  = (initrd->len + PAGE_SIZE - 1) >> PAGE_SHIFT;

    for ( pfn = pfn_start; pfn < (pfn_start + nr_pages); pfn++ )
    {
        if ( initrd->type == INITRD_mem )
        {
            xc_copy_to_domain_page(
                xc_handle, dom, phys_to_mach[pfn],
                &initrd->u.mem_addr[(pfn - pfn_start) << PAGE_SHIFT]);
        }
        else
        {
            if ( gzread(initrd->u.file_handle, page, PAGE_SIZE) == -1 )
            {
                PERROR("Error reading initrd image, could not");
                return -EINVAL;
            }
            xc_copy_to_domain_page(xc_handle, dom, phys_to_mach[pfn], page);
        }
    }

    return 0;
}

#define alloc_pt(ltab, vltab, pltab)                                    \
do {                                                                    \
    pltab = ppt_alloc++;                                                \
    ltab = (uint64_t)page_array[pltab] << PAGE_SHIFT;                   \
    pltab <<= PAGE_SHIFT;                                               \
    if ( vltab != NULL )                                                \
        munmap(vltab, PAGE_SIZE);                                       \
    if ( (vltab = xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,       \
                                       PROT_READ|PROT_WRITE,            \
                                       ltab >> PAGE_SHIFT)) == NULL )   \
        goto error_out;                                                 \
    memset(vltab, 0x0, PAGE_SIZE);                                      \
} while ( 0 )

#if defined(__i386__)

static int setup_pg_tables(int xc_handle, uint32_t dom,
                           vcpu_guest_context_t *ctxt,
                           unsigned long dsi_v_start,
                           unsigned long v_end,
                           xen_pfn_t *page_array,
                           unsigned long vpt_start,
                           unsigned long vpt_end,
                           unsigned shadow_mode_enabled)
{
    l1_pgentry_t *vl1tab=NULL, *vl1e=NULL;
    l2_pgentry_t *vl2tab=NULL, *vl2e=NULL;
    unsigned long l1tab = 0, pl1tab;
    unsigned long l2tab = 0, pl2tab;
    unsigned long ppt_alloc;
    unsigned long count;

    ppt_alloc = (vpt_start - dsi_v_start) >> PAGE_SHIFT;
    alloc_pt(l2tab, vl2tab, pl2tab);
    vl2e = &vl2tab[l2_table_offset(dsi_v_start)];
    if (shadow_mode_enabled)
        ctxt->ctrlreg[3] = xen_pfn_to_cr3(pl2tab >> PAGE_SHIFT);
    else
        ctxt->ctrlreg[3] = xen_pfn_to_cr3(l2tab >> PAGE_SHIFT);

    for ( count = 0; count < ((v_end - dsi_v_start) >> PAGE_SHIFT); count++ )
    {
        if ( ((unsigned long)vl1e & (PAGE_SIZE-1)) == 0 )
        {
            alloc_pt(l1tab, vl1tab, pl1tab);
            vl1e = &vl1tab[l1_table_offset(dsi_v_start + (count<<PAGE_SHIFT))];
            if (shadow_mode_enabled)
                *vl2e = pl1tab | L2_PROT;
            else
                *vl2e = l1tab | L2_PROT;
            vl2e++;
        }

        if ( shadow_mode_enabled )
        {
            *vl1e = (count << PAGE_SHIFT) | L1_PROT;
        }
        else
        {
            *vl1e = (page_array[count] << PAGE_SHIFT) | L1_PROT;
            if ( (count >= ((vpt_start-dsi_v_start)>>PAGE_SHIFT)) &&
                 (count <  ((vpt_end  -dsi_v_start)>>PAGE_SHIFT)) )
                *vl1e &= ~_PAGE_RW;
        }
        vl1e++;
    }
    munmap(vl1tab, PAGE_SIZE);
    munmap(vl2tab, PAGE_SIZE);
    return 0;

 error_out:
    if (vl1tab)
        munmap(vl1tab, PAGE_SIZE);
    if (vl2tab)
        munmap(vl2tab, PAGE_SIZE);
    return -1;
}

static int setup_pg_tables_pae(int xc_handle, uint32_t dom,
                               vcpu_guest_context_t *ctxt,
                               unsigned long dsi_v_start,
                               unsigned long v_end,
                               xen_pfn_t *page_array,
                               unsigned long vpt_start,
                               unsigned long vpt_end,
                               unsigned shadow_mode_enabled,
                               unsigned pae_mode)
{
    l1_pgentry_64_t *vl1tab = NULL, *vl1e = NULL;
    l2_pgentry_64_t *vl2tab = NULL, *vl2e = NULL;
    l3_pgentry_64_t *vl3tab = NULL, *vl3e = NULL;
    uint64_t l1tab, l2tab, l3tab, pl1tab, pl2tab, pl3tab;
    unsigned long ppt_alloc, count, nmfn;

    /* First allocate page for page dir. */
    ppt_alloc = (vpt_start - dsi_v_start) >> PAGE_SHIFT;

    if ( pae_mode == PAEKERN_extended_cr3 )
    {
        ctxt->vm_assist |= (1UL << VMASST_TYPE_pae_extended_cr3);
    }
    else if ( page_array[ppt_alloc] > 0xfffff )
    {
        nmfn = xc_make_page_below_4G(xc_handle, dom, page_array[ppt_alloc]);
        if ( nmfn == 0 )
        {
            DPRINTF("Couldn't get a page below 4GB :-(\n");
            goto error_out;
        }
        page_array[ppt_alloc] = nmfn;
    }

    alloc_pt(l3tab, vl3tab, pl3tab);
    vl3e = &vl3tab[l3_table_offset_pae(dsi_v_start)];
    if (shadow_mode_enabled)
        ctxt->ctrlreg[3] = xen_pfn_to_cr3(pl3tab >> PAGE_SHIFT);
    else
        ctxt->ctrlreg[3] = xen_pfn_to_cr3(l3tab >> PAGE_SHIFT);

    for ( count = 0; count < ((v_end - dsi_v_start) >> PAGE_SHIFT); count++)
    {
        if ( !((unsigned long)vl1e & (PAGE_SIZE-1)) )
        {
            if ( !((unsigned long)vl2e & (PAGE_SIZE-1)) )
            {
                alloc_pt(l2tab, vl2tab, pl2tab);
                vl2e = &vl2tab[l2_table_offset_pae(
                    dsi_v_start + (count << PAGE_SHIFT))];
                if (shadow_mode_enabled)
                    *vl3e = pl2tab | L3_PROT;
                else
                    *vl3e++ = l2tab | L3_PROT;
            }

            alloc_pt(l1tab, vl1tab, pl1tab);
            vl1e = &vl1tab[l1_table_offset_pae(
                dsi_v_start + (count << PAGE_SHIFT))];
            if (shadow_mode_enabled)
                *vl2e = pl1tab | L2_PROT;
            else
                *vl2e++ = l1tab | L2_PROT;
        }

        if ( shadow_mode_enabled )
        {
            *vl1e = (count << PAGE_SHIFT) | L1_PROT;
        }
        else
        {
            *vl1e = ((uint64_t)page_array[count] << PAGE_SHIFT) | L1_PROT;
            if ( (count >= ((vpt_start-dsi_v_start)>>PAGE_SHIFT)) &&
                 (count <  ((vpt_end  -dsi_v_start)>>PAGE_SHIFT)) )
                *vl1e &= ~_PAGE_RW;
        }
        vl1e++;
    }

    /* Xen requires a mid-level pgdir mapping 0xC0000000 region. */
    if ( (vl3tab[3] & _PAGE_PRESENT) == 0 )
    {
        alloc_pt(l2tab, vl2tab, pl2tab);
        vl3tab[3] = l2tab | L3_PROT;
    }

    munmap(vl1tab, PAGE_SIZE);
    munmap(vl2tab, PAGE_SIZE);
    munmap(vl3tab, PAGE_SIZE);
    return 0;

 error_out:
    if (vl1tab)
        munmap(vl1tab, PAGE_SIZE);
    if (vl2tab)
        munmap(vl2tab, PAGE_SIZE);
    if (vl3tab)
        munmap(vl3tab, PAGE_SIZE);
    return -1;
}

#endif

#if defined(__x86_64__)

static int setup_pg_tables_64(int xc_handle, uint32_t dom,
                              vcpu_guest_context_t *ctxt,
                              unsigned long dsi_v_start,
                              unsigned long v_end,
                              xen_pfn_t *page_array,
                              unsigned long vpt_start,
                              unsigned long vpt_end,
                              int shadow_mode_enabled)
{
    l1_pgentry_t *vl1tab=NULL, *vl1e=NULL;
    l2_pgentry_t *vl2tab=NULL, *vl2e=NULL;
    l3_pgentry_t *vl3tab=NULL, *vl3e=NULL;
    l4_pgentry_t *vl4tab=NULL, *vl4e=NULL;
    unsigned long l2tab = 0, pl2tab;
    unsigned long l1tab = 0, pl1tab;
    unsigned long l3tab = 0, pl3tab;
    unsigned long l4tab = 0, pl4tab;
    unsigned long ppt_alloc;
    unsigned long count;

    /* First allocate page for page dir. */
    ppt_alloc = (vpt_start - dsi_v_start) >> PAGE_SHIFT;
    alloc_pt(l4tab, vl4tab, pl4tab);
    vl4e = &vl4tab[l4_table_offset(dsi_v_start)];
    if (shadow_mode_enabled)
        ctxt->ctrlreg[3] = xen_pfn_to_cr3(pl4tab >> PAGE_SHIFT);
    else
        ctxt->ctrlreg[3] = xen_pfn_to_cr3(l4tab >> PAGE_SHIFT);

    for ( count = 0; count < ((v_end-dsi_v_start)>>PAGE_SHIFT); count++)
    {
        if ( !((unsigned long)vl1e & (PAGE_SIZE-1)) )
        {
            alloc_pt(l1tab, vl1tab, pl1tab);

            if ( !((unsigned long)vl2e & (PAGE_SIZE-1)) )
            {
                alloc_pt(l2tab, vl2tab, pl2tab);
                if ( !((unsigned long)vl3e & (PAGE_SIZE-1)) )
                {
                    alloc_pt(l3tab, vl3tab, pl3tab);
                    vl3e = &vl3tab[l3_table_offset(dsi_v_start + (count<<PAGE_SHIFT))];
                    if (shadow_mode_enabled)
                        *vl4e = pl3tab | L4_PROT;
                    else
                        *vl4e = l3tab | L4_PROT;
                    vl4e++;
                }
                vl2e = &vl2tab[l2_table_offset(dsi_v_start + (count<<PAGE_SHIFT))];
                if (shadow_mode_enabled)
                    *vl3e = pl2tab | L3_PROT;
                else
                    *vl3e = l2tab | L3_PROT;
                vl3e++;
            }
            vl1e = &vl1tab[l1_table_offset(dsi_v_start + (count<<PAGE_SHIFT))];
            if (shadow_mode_enabled)
                *vl2e = pl1tab | L2_PROT;
            else
                *vl2e = l1tab | L2_PROT;
            vl2e++;
        }

        if ( shadow_mode_enabled )
        {
            *vl1e = (count << PAGE_SHIFT) | L1_PROT;
        }
        else
        {
            *vl1e = (page_array[count] << PAGE_SHIFT) | L1_PROT;
            if ( (count >= ((vpt_start-dsi_v_start)>>PAGE_SHIFT)) &&
                 (count <  ((vpt_end  -dsi_v_start)>>PAGE_SHIFT)) )
                {
                    *vl1e &= ~_PAGE_RW;
                }
        }
        vl1e++;
    }

    munmap(vl1tab, PAGE_SIZE);
    munmap(vl2tab, PAGE_SIZE);
    munmap(vl3tab, PAGE_SIZE);
    munmap(vl4tab, PAGE_SIZE);
    return 0;

 error_out:
    if (vl1tab)
        munmap(vl1tab, PAGE_SIZE);
    if (vl2tab)
        munmap(vl2tab, PAGE_SIZE);
    if (vl3tab)
        munmap(vl3tab, PAGE_SIZE);
    if (vl4tab)
        munmap(vl4tab, PAGE_SIZE);
    return -1;
}
#endif

#ifdef __ia64__
extern unsigned long xc_ia64_fpsr_default(void);

static int setup_guest(int xc_handle,
                       uint32_t dom,
                       const char *image, unsigned long image_size,
                       struct initrd_info *initrd,
                       unsigned long nr_pages,
                       unsigned long *pvsi, unsigned long *pvke,
                       unsigned long *pvss, vcpu_guest_context_t *ctxt,
                       const char *cmdline,
                       unsigned long shared_info_frame,
                       unsigned long flags,
                       unsigned int store_evtchn, unsigned long *store_mfn,
                       unsigned int console_evtchn, unsigned long *console_mfn,
                       uint32_t required_features[XENFEAT_NR_SUBMAPS])
{
    xen_pfn_t *page_array = NULL;
    struct load_funcs load_funcs;
    struct domain_setup_info dsi;
    unsigned long vinitrd_start;
    unsigned long vinitrd_end;
    unsigned long v_end;
    unsigned long start_page, pgnr;
    start_info_t *start_info;
    unsigned long start_info_mpa;
    struct xen_ia64_boot_param *bp;
    shared_info_t *shared_info;
    int i;
    DECLARE_DOMCTL;
    int rc;

    rc = probeimageformat(image, image_size, &load_funcs);
    if ( rc != 0 )
        goto error_out;

    memset(&dsi, 0, sizeof(struct domain_setup_info));

    rc = (load_funcs.parseimage)(image, image_size, &dsi);
    if ( rc != 0 )
        goto error_out;

    dsi.v_start      = round_pgdown(dsi.v_start);
    vinitrd_start    = round_pgup(dsi.v_end);
    vinitrd_end      = vinitrd_start + initrd->len;
    v_end            = round_pgup(vinitrd_end);
    start_info_mpa = (nr_pages - 3) << PAGE_SHIFT;

    /* Build firmware.  */
    memset(&domctl.u.arch_setup, 0, sizeof(domctl.u.arch_setup));
    domctl.u.arch_setup.flags = 0;
    domctl.u.arch_setup.bp = start_info_mpa + sizeof (start_info_t);
    domctl.u.arch_setup.maxmem = (nr_pages - 3) << PAGE_SHIFT;
    domctl.cmd = XEN_DOMCTL_arch_setup;
    domctl.domain = (domid_t)dom;
    if ( xc_domctl(xc_handle, &domctl) )
        goto error_out;

    start_page = dsi.v_start >> PAGE_SHIFT;
    pgnr = (v_end - dsi.v_start) >> PAGE_SHIFT;
    if ( (page_array = malloc(pgnr * sizeof(xen_pfn_t))) == NULL )
    {
        PERROR("Could not allocate memory");
        goto error_out;
    }

    if ( xc_ia64_get_pfn_list(xc_handle, dom, page_array,
                              start_page, pgnr) != pgnr )
    {
        PERROR("Could not get the page frame list");
        goto error_out;
    }

    IPRINTF("VIRTUAL MEMORY ARRANGEMENT:\n"
           " Loaded kernel: %p->%p\n"
           " Init. ramdisk: %p->%p\n"
           " TOTAL:         %p->%p\n",
           _p(dsi.v_kernstart), _p(dsi.v_kernend),
           _p(vinitrd_start),   _p(vinitrd_end),
           _p(dsi.v_start),     _p(v_end));
    IPRINTF(" ENTRY ADDRESS: %p\n", _p(dsi.v_kernentry));

    (load_funcs.loadimage)(image, image_size, xc_handle, dom, page_array,
                           &dsi);

    if ( load_initrd(xc_handle, dom, initrd,
                     vinitrd_start - dsi.v_start, page_array) )
        goto error_out;

    *pvke = dsi.v_kernentry;

    /* Now need to retrieve machine pfn for system pages:
     *  start_info/store/console
     */
    pgnr = 3;
    if ( xc_ia64_get_pfn_list(xc_handle, dom, page_array,
                              nr_pages - 3, pgnr) != pgnr )
    {
        PERROR("Could not get page frame for xenstore");
        goto error_out;
    }

    *store_mfn = page_array[1];
    *console_mfn = page_array[2];
    IPRINTF("start_info: 0x%lx at 0x%lx, "
           "store_mfn: 0x%lx at 0x%lx, "
           "console_mfn: 0x%lx at 0x%lx\n",
           page_array[0], nr_pages - 3,
           *store_mfn,    nr_pages - 2,
           *console_mfn,  nr_pages - 1);

    start_info = xc_map_foreign_range(
        xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE, page_array[0]);
    memset(start_info, 0, sizeof(*start_info));
    rc = xc_version(xc_handle, XENVER_version, NULL);
    sprintf(start_info->magic, "xen-%i.%i-ia64", rc >> 16, rc & (0xFFFF));
    start_info->flags        = flags;
    start_info->store_mfn    = nr_pages - 2;
    start_info->store_evtchn = store_evtchn;
    start_info->console.domU.mfn   = nr_pages - 1;
    start_info->console.domU.evtchn = console_evtchn;
    start_info->nr_pages       = nr_pages; // FIXME?: nr_pages - 2 ????

    bp = (struct xen_ia64_boot_param *)(start_info + 1);
    bp->command_line = start_info_mpa + offsetof(start_info_t, cmd_line);
    if ( cmdline != NULL )
    {
        strncpy((char *)start_info->cmd_line, cmdline, MAX_GUEST_CMDLINE);
        start_info->cmd_line[MAX_GUEST_CMDLINE - 1] = 0;
    }
    if ( initrd->len != 0 )
    {
        bp->initrd_start    = vinitrd_start;
        bp->initrd_size     = initrd->len;
    }
    ctxt->user_regs.r28 = start_info_mpa + sizeof (start_info_t);
    munmap(start_info, PAGE_SIZE);

    /* shared_info page starts its life empty. */
    shared_info = xc_map_foreign_range(
        xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE, shared_info_frame);
    printf("shared_info = %p, err=%s frame=%lx\n",
           shared_info, strerror (errno), shared_info_frame);
    //memset(shared_info, 0, PAGE_SIZE);
    /* Mask all upcalls... */
    for ( i = 0; i < MAX_VIRT_CPUS; i++ )
        shared_info->vcpu_info[i].evtchn_upcall_mask = 1;
    shared_info->arch.start_info_pfn = nr_pages - 3;

    munmap(shared_info, PAGE_SIZE);

    free(page_array);
    return 0;

 error_out:
    free(page_array);
    return -1;
}
#else /* x86 */

/* Check if the platform supports the guest kernel format */
static int compat_check(int xc_handle, struct domain_setup_info *dsi)
{
    xen_capabilities_info_t xen_caps = "";

    if (xc_version(xc_handle, XENVER_capabilities, &xen_caps) != 0) {
        ERROR("Cannot determine host capabilities.");
        return 0;
    }

    if (strstr(xen_caps, "xen-3.0-x86_32p")) {
        if (dsi->pae_kernel == PAEKERN_no) {
            ERROR("Non PAE-kernel on PAE host.");
            return 0;
        }
    } else if (dsi->pae_kernel != PAEKERN_no) {
        ERROR("PAE-kernel on non-PAE host.");
        return 0;
    }

    return 1;
}

static inline int increment_ulong(unsigned long *pval, unsigned long inc)
{
    if ( inc >= -*pval )
    {
        ERROR("Value wrapped to zero: image too large?");
        return 0;
    }
    *pval += inc;
    return 1;
}

static int setup_guest(int xc_handle,
                       uint32_t dom,
                       const char *image, unsigned long image_size,
                       struct initrd_info *initrd,
                       unsigned long nr_pages,
                       unsigned long *pvsi, unsigned long *pvke,
                       unsigned long *pvss, vcpu_guest_context_t *ctxt,
                       const char *cmdline,
                       unsigned long shared_info_frame,
                       unsigned long flags,
                       unsigned int store_evtchn, unsigned long *store_mfn,
                       unsigned int console_evtchn, unsigned long *console_mfn,
                       uint32_t required_features[XENFEAT_NR_SUBMAPS])
{
    xen_pfn_t *page_array = NULL;
    unsigned long count, i;
    unsigned long long hypercall_page;
    int hypercall_page_defined;
    start_info_t *start_info;
    shared_info_t *shared_info;
    xc_mmu_t *mmu = NULL;
    const char *p;
    DECLARE_DOMCTL;
    int rc;

    unsigned long nr_pt_pages;
    unsigned long physmap_pfn;
    xen_pfn_t *physmap, *physmap_e;

    struct load_funcs load_funcs;
    struct domain_setup_info dsi;
    unsigned long vinitrd_start;
    unsigned long vphysmap_start;
    unsigned long vstartinfo_start;
    unsigned long vstoreinfo_start;
    unsigned long vconsole_start;
    unsigned long vsharedinfo_start = 0; /* XXX gcc */
    unsigned long vstack_start;
    unsigned long vstack_end;
    unsigned long vpt_start;
    unsigned long vpt_end;
    unsigned long v_end;
    unsigned long guest_store_mfn, guest_console_mfn, guest_shared_info_mfn;
    unsigned long shadow_mode_enabled;
    uint32_t supported_features[XENFEAT_NR_SUBMAPS] = { 0, };

    rc = probeimageformat(image, image_size, &load_funcs);
    if ( rc != 0 )
        goto error_out;

    memset(&dsi, 0, sizeof(struct domain_setup_info));

    rc = (load_funcs.parseimage)(image, image_size, &dsi);
    if ( rc != 0 )
        goto error_out;

    if ( (dsi.v_start & (PAGE_SIZE-1)) != 0 )
    {
        PERROR("Guest OS must load to a page boundary.");
        goto error_out;
    }

    if (!compat_check(xc_handle, &dsi))
        goto error_out;

    /* Parse and validate kernel features. */
    if ( (p = xen_elfnote_string(&dsi, XEN_ELFNOTE_FEATURES)) != NULL )
    {
        if ( !parse_features(p, supported_features, required_features) )
        {
            ERROR("Failed to parse guest kernel features.");
            goto error_out;
        }

        IPRINTF("Supported features  = { %08x }.\n", supported_features[0]);
        IPRINTF("Required features   = { %08x }.\n", required_features[0]);
    }

    for ( i = 0; i < XENFEAT_NR_SUBMAPS; i++ )
    {
        if ( (supported_features[i] & required_features[i]) !=
             required_features[i] )
        {
            ERROR("Guest kernel does not support a required feature.");
            goto error_out;
        }
    }

    shadow_mode_enabled = test_feature_bit(XENFEAT_auto_translated_physmap,
                                           required_features);

    /*
     * Why do we need this? The number of page-table frames depends on the
     * size of the bootstrap address space. But the size of the address space
     * depends on the number of page-table frames (since each one is mapped
     * read-only). We have a pair of simultaneous equations in two unknowns,
     * which we solve by exhaustive search.
     */
    v_end = round_pgup(dsi.v_end);
    if ( v_end == 0 )
    {
        ERROR("End of mapped kernel image too close to end of memory");
        goto error_out;
    }
    vinitrd_start = v_end;
    if ( !increment_ulong(&v_end, round_pgup(initrd->len)) )
        goto error_out;
    vphysmap_start = v_end;
    if ( !increment_ulong(&v_end, round_pgup(nr_pages * sizeof(long))) )
        goto error_out;
    vstartinfo_start = v_end;
    if ( !increment_ulong(&v_end, PAGE_SIZE) )
        goto error_out;
    vstoreinfo_start = v_end;
    if ( !increment_ulong(&v_end, PAGE_SIZE) )
        goto error_out;
    vconsole_start = v_end;
    if ( !increment_ulong(&v_end, PAGE_SIZE) )
        goto error_out;
    if ( shadow_mode_enabled ) {
        vsharedinfo_start = v_end;
        if ( !increment_ulong(&v_end, PAGE_SIZE) )
            goto error_out;
    }
    vpt_start = v_end;

    for ( nr_pt_pages = 2; ; nr_pt_pages++ )
    {
        /* vpt_end = vpt_staret + (nr_pt_pages * PAGE_SIZE); */
        vpt_end = vpt_start;
        if ( !increment_ulong(&vpt_end, nr_pt_pages * PAGE_SIZE) )
            goto error_out;

        vstack_start = vpt_end;
        /* vstack_end = vstack_start + PAGE_SIZE; */
        vstack_end = vstack_start;
        if ( !increment_ulong(&vstack_end, PAGE_SIZE) )
            goto error_out;

        /* v_end = (vstack_end + (1UL<<22)-1) & ~((1UL<<22)-1); */
        v_end = vstack_end;
        if ( !increment_ulong(&v_end, (1UL<<22)-1) )
            goto error_out;
        v_end &= ~((1UL<<22)-1);

        if ( (v_end - vstack_end) < (512UL << 10) )
        {
            /* Add extra 4MB to get >= 512kB padding. */
            if ( !increment_ulong(&v_end, 1UL << 22) )
                goto error_out;
        }

#define NR(_l,_h,_s)                                                    \
    (((((unsigned long)(_h) + ((1UL<<(_s))-1)) & ~((1UL<<(_s))-1)) -    \
    ((unsigned long)(_l) & ~((1UL<<(_s))-1))) >> (_s))
#if defined(__i386__)
        if ( dsi.pae_kernel != PAEKERN_no )
        {
            if ( (1 + /* # L3 */
                  NR(dsi.v_start, v_end, L3_PAGETABLE_SHIFT_PAE) + /* # L2 */
                  NR(dsi.v_start, v_end, L2_PAGETABLE_SHIFT_PAE) + /* # L1 */
                  /* Include a fourth mid-level page directory for Xen. */
                  (v_end <= (3 << L3_PAGETABLE_SHIFT_PAE)))
                  <= nr_pt_pages )
                break;
        }
        else
        {
            if ( (1 + /* # L2 */
                  NR(dsi.v_start, v_end, L2_PAGETABLE_SHIFT)) /* # L1 */
                 <= nr_pt_pages )
                break;
        }
#elif defined(__x86_64__)
        if ( (1 + /* # L4 */
              NR(dsi.v_start, v_end, L4_PAGETABLE_SHIFT) + /* # L3 */
              NR(dsi.v_start, v_end, L3_PAGETABLE_SHIFT) + /* # L2 */
              NR(dsi.v_start, v_end, L2_PAGETABLE_SHIFT))  /* # L1 */
             <= nr_pt_pages )
            break;
#endif
    }

    IPRINTF("VIRTUAL MEMORY ARRANGEMENT:\n");
    IPRINTF(" Loaded kernel:    %p->%p\n", _p(dsi.v_kernstart),
           _p(dsi.v_kernend));
    if ( initrd->len )
        IPRINTF(" Initial ramdisk:  %p->%p\n", _p(vinitrd_start),
               _p(vinitrd_start + initrd->len));
    IPRINTF(" Phys-Mach map:    %p\n", _p(vphysmap_start));
    IPRINTF(" Start info:       %p\n", _p(vstartinfo_start));
    IPRINTF(" Store page:       %p\n", _p(vstoreinfo_start));
    IPRINTF(" Console page:     %p\n", _p(vconsole_start));
    if ( shadow_mode_enabled )
        IPRINTF(" Shared Info page: %p\n", _p(vsharedinfo_start));
    IPRINTF(" Page tables:      %p\n", _p(vpt_start));
    IPRINTF(" Boot stack:       %p\n", _p(vstack_start));
    IPRINTF(" TOTAL:            %p->%p\n", _p(dsi.v_start), _p(v_end));
    IPRINTF(" ENTRY ADDRESS:    %p\n", _p(dsi.v_kernentry));

    if ( ((v_end - dsi.v_start)>>PAGE_SHIFT) > nr_pages )
    {
        PERROR("Initial guest OS requires too much space\n"
               "(%pMB is greater than %luMB limit)\n",
               _p((v_end-dsi.v_start)>>20), nr_pages>>(20-PAGE_SHIFT));
        goto error_out;
    }

    if ( (page_array = malloc(nr_pages * sizeof(unsigned long))) == NULL )
    {
        PERROR("Could not allocate memory");
        goto error_out;
    }

    if ( xc_get_pfn_list(xc_handle, dom, page_array, nr_pages) != nr_pages )
    {
        PERROR("Could not get the page frame list");
        goto error_out;
    }

    rc = (load_funcs.loadimage)(image, image_size,
                           xc_handle, dom, page_array,
                           &dsi);
    if ( rc != 0 )
        goto error_out;

    if ( load_initrd(xc_handle, dom, initrd,
                     vinitrd_start - dsi.v_start, page_array) )
        goto error_out;

    /* setup page tables */
#if defined(__i386__)
    if (dsi.pae_kernel != PAEKERN_no)
        rc = setup_pg_tables_pae(xc_handle, dom, ctxt,
                                 dsi.v_start, v_end,
                                 page_array, vpt_start, vpt_end,
                                 shadow_mode_enabled, dsi.pae_kernel);
    else
        rc = setup_pg_tables(xc_handle, dom, ctxt,
                             dsi.v_start, v_end,
                             page_array, vpt_start, vpt_end,
                             shadow_mode_enabled);
#endif
#if defined(__x86_64__)
    rc = setup_pg_tables_64(xc_handle, dom, ctxt,
                            dsi.v_start, v_end,
                            page_array, vpt_start, vpt_end,
                            shadow_mode_enabled);
#endif
    if (0 != rc)
        goto error_out;

#if defined(__i386__)
    /*
     * Pin down l2tab addr as page dir page - causes hypervisor to provide
     * correct protection for the page
     */
    if ( !shadow_mode_enabled )
    {
        if ( dsi.pae_kernel != PAEKERN_no )
        {
            if ( pin_table(xc_handle, MMUEXT_PIN_L3_TABLE,
                           xen_cr3_to_pfn(ctxt->ctrlreg[3]), dom) )
                goto error_out;
        }
        else
        {
            if ( pin_table(xc_handle, MMUEXT_PIN_L2_TABLE,
                           xen_cr3_to_pfn(ctxt->ctrlreg[3]), dom) )
                goto error_out;
        }
    }
#endif

#if defined(__x86_64__)
    /*
     * Pin down l4tab addr as page dir page - causes hypervisor to  provide
     * correct protection for the page
     */
    if ( pin_table(xc_handle, MMUEXT_PIN_L4_TABLE,
                   xen_cr3_to_pfn(ctxt->ctrlreg[3]), dom) )
        goto error_out;
#endif

    if ( (mmu = xc_init_mmu_updates(xc_handle, dom)) == NULL )
        goto error_out;

    /* Write the phys->machine and machine->phys table entries. */
    physmap_pfn = (vphysmap_start - dsi.v_start) >> PAGE_SHIFT;
    physmap = physmap_e = xc_map_foreign_range(
        xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE,
        page_array[physmap_pfn++]);

    for ( count = 0; count < nr_pages; count++ )
    {
        if ( xc_add_mmu_update(
            xc_handle, mmu,
            ((uint64_t)page_array[count] << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE,
            count) )
        {
            DPRINTF("m2p update failure p=%lx m=%"PRIx64"\n",
                    count, (uint64_t)page_array[count]);
            munmap(physmap, PAGE_SIZE);
            goto error_out;
        }
        *physmap_e++ = page_array[count];
        if ( ((unsigned long)physmap_e & (PAGE_SIZE-1)) == 0 )
        {
            munmap(physmap, PAGE_SIZE);
            physmap = physmap_e = xc_map_foreign_range(
                xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE,
                page_array[physmap_pfn++]);
        }
    }
    munmap(physmap, PAGE_SIZE);

    /* Send the page update requests down to the hypervisor. */
    if ( xc_finish_mmu_updates(xc_handle, mmu) )
        goto error_out;

    if ( shadow_mode_enabled )
    {
        struct xen_add_to_physmap xatp;

        /* Enable shadow translate mode */
        if ( xc_shadow_control(xc_handle, dom,
                               XEN_DOMCTL_SHADOW_OP_ENABLE_TRANSLATE,
                               NULL, 0, NULL, 0, NULL) < 0 )
        {
            PERROR("Could not enable translation mode");
            goto error_out;
        }

        guest_shared_info_mfn = (vsharedinfo_start-dsi.v_start) >> PAGE_SHIFT;

        /* Map shared info frame into guest physmap. */
        xatp.domid = dom;
        xatp.space = XENMAPSPACE_shared_info;
        xatp.idx   = 0;
        xatp.gpfn  = guest_shared_info_mfn;
        rc = xc_memory_op(xc_handle, XENMEM_add_to_physmap, &xatp);
        if ( rc != 0 )
        {
            PERROR("Cannot map shared info pfn");
            goto error_out;
        }

        /* Map grant table frames into guest physmap. */
        for ( i = 0; ; i++ )
        {
            xatp.domid = dom;
            xatp.space = XENMAPSPACE_grant_table;
            xatp.idx   = i;
            xatp.gpfn  = nr_pages + i;
            rc = xc_memory_op(xc_handle, XENMEM_add_to_physmap, &xatp);
            if ( rc != 0 )
            {
                if ( errno == EINVAL )
                    break; /* done all grant tables */
                PERROR("Cannot map grant table pfn");
                goto error_out;
            }
        }
    }
    else
    {
        guest_shared_info_mfn = shared_info_frame;
    }

    *store_mfn = page_array[(vstoreinfo_start-dsi.v_start) >> PAGE_SHIFT];
    *console_mfn = page_array[(vconsole_start-dsi.v_start) >> PAGE_SHIFT];
    if ( xc_clear_domain_page(xc_handle, dom, *store_mfn) ||
         xc_clear_domain_page(xc_handle, dom, *console_mfn) )
        goto error_out;
    if ( shadow_mode_enabled )
    {
        guest_store_mfn = (vstoreinfo_start-dsi.v_start) >> PAGE_SHIFT;
        guest_console_mfn = (vconsole_start-dsi.v_start) >> PAGE_SHIFT;
    }
    else
    {
        guest_store_mfn = *store_mfn;
        guest_console_mfn = *console_mfn;
    }

    start_info = xc_map_foreign_range(
        xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE,
        page_array[(vstartinfo_start-dsi.v_start)>>PAGE_SHIFT]);
    /*shared_info, start_info */
    memset(start_info, 0, sizeof(*start_info));
    rc = xc_version(xc_handle, XENVER_version, NULL);
    sprintf(start_info->magic, "xen-%i.%i-x86_%d%s",
            rc >> 16, rc & (0xFFFF), (unsigned int)sizeof(long)*8,
            (dsi.pae_kernel != PAEKERN_no) ? "p" : "");
    start_info->nr_pages     = nr_pages;
    start_info->shared_info  = guest_shared_info_mfn << PAGE_SHIFT;
    start_info->flags        = flags;
    start_info->pt_base      = vpt_start;
    start_info->nr_pt_frames = nr_pt_pages;
    start_info->mfn_list     = vphysmap_start;
    start_info->store_mfn    = guest_store_mfn;
    start_info->store_evtchn = store_evtchn;
    start_info->console.domU.mfn   = guest_console_mfn;
    start_info->console.domU.evtchn = console_evtchn;
    if ( initrd->len != 0 )
    {
        start_info->mod_start    = vinitrd_start;
        start_info->mod_len      = initrd->len;
    }
    if ( cmdline != NULL )
    {
        strncpy((char *)start_info->cmd_line, cmdline, MAX_GUEST_CMDLINE);
        start_info->cmd_line[MAX_GUEST_CMDLINE-1] = '\0';
    }
    munmap(start_info, PAGE_SIZE);

    /* shared_info page starts its life empty. */
    shared_info = xc_map_foreign_range(
        xc_handle, dom, PAGE_SIZE, PROT_READ|PROT_WRITE, shared_info_frame);
    memset(shared_info, 0, PAGE_SIZE);
    /* Mask all upcalls... */
    for ( i = 0; i < MAX_VIRT_CPUS; i++ )
        shared_info->vcpu_info[i].evtchn_upcall_mask = 1;

    munmap(shared_info, PAGE_SIZE);

    /* Send the page update requests down to the hypervisor. */
    if ( xc_finish_mmu_updates(xc_handle, mmu) )
        goto error_out;

    hypercall_page = xen_elfnote_numeric(&dsi, XEN_ELFNOTE_HYPERCALL_PAGE,
                                         &hypercall_page_defined);
    if ( hypercall_page_defined )
    {
        unsigned long long pfn = (hypercall_page - dsi.v_start) >> PAGE_SHIFT;
        if ( pfn >= nr_pages )
            goto error_out;
        domctl.domain = (domid_t)dom;
        domctl.u.hypercall_init.gmfn   = shadow_mode_enabled ?
            pfn : page_array[pfn];
        domctl.cmd = XEN_DOMCTL_hypercall_init;
        if ( xc_domctl(xc_handle, &domctl) )
            goto error_out;
    }

    free(mmu);
    free(page_array);

    *pvsi = vstartinfo_start;
    *pvss = vstack_start;
    *pvke = dsi.v_kernentry;

    return 0;

 error_out:
    free(mmu);
    free(page_array);
    return -1;
}
#endif

static int xc_linux_build_internal(int xc_handle,
                                   uint32_t domid,
                                   char *image,
                                   unsigned long image_size,
                                   struct initrd_info *initrd,
                                   const char *cmdline,
                                   const char *features,
                                   unsigned long flags,
                                   unsigned int store_evtchn,
                                   unsigned long *store_mfn,
                                   unsigned int console_evtchn,
                                   unsigned long *console_mfn)
{
    struct xen_domctl launch_domctl;
    DECLARE_DOMCTL;
    int rc, i;
    vcpu_guest_context_t st_ctxt, *ctxt = &st_ctxt;
    unsigned long nr_pages;
    unsigned long vstartinfo_start, vkern_entry, vstack_start;
    uint32_t      features_bitmap[XENFEAT_NR_SUBMAPS] = { 0, };

    if ( features != NULL )
    {
        if ( !parse_features(features, features_bitmap, NULL) )
        {
            PERROR("Failed to parse configured features\n");
            goto error_out;
        }
    }

    if ( (nr_pages = get_tot_pages(xc_handle, domid)) < 0 )
    {
        PERROR("Could not find total pages for domain");
        goto error_out;
    }

#ifdef VALGRIND
    memset(&st_ctxt, 0, sizeof(st_ctxt));
#endif

    if ( lock_pages(&st_ctxt, sizeof(st_ctxt) ) )
    {
        PERROR("%s: ctxt lock failed", __func__);
        return 1;
    }

    domctl.cmd = XEN_DOMCTL_getdomaininfo;
    domctl.domain = (domid_t)domid;
    if ( (xc_domctl(xc_handle, &domctl) < 0) ||
         ((uint16_t)domctl.domain != domid) )
    {
        PERROR("Could not get info on domain");
        goto error_out;
    }

    memset(ctxt, 0, sizeof(*ctxt));

    if ( setup_guest(xc_handle, domid, image, image_size,
                     initrd,
                     nr_pages,
                     &vstartinfo_start, &vkern_entry,
                     &vstack_start, ctxt, cmdline,
                     domctl.u.getdomaininfo.shared_info_frame,
                     flags, store_evtchn, store_mfn,
                     console_evtchn, console_mfn,
                     features_bitmap) < 0 )
    {
        ERROR("Error constructing guest OS");
        goto error_out;
    }

#ifdef __ia64__
    /* based on new_thread in xen/arch/ia64/domain.c */
    ctxt->flags = 0;
    ctxt->user_regs.cr_ipsr = 0; /* all necessary bits filled by hypervisor */
    ctxt->user_regs.cr_iip = vkern_entry;
    ctxt->user_regs.cr_ifs = 1UL << 63;
    ctxt->user_regs.ar_fpsr = xc_ia64_fpsr_default();
    i = 0; /* silence unused variable warning */
#else /* x86 */
    /*
     * Initial register values:
     *  DS,ES,FS,GS = FLAT_KERNEL_DS
     *       CS:EIP = FLAT_KERNEL_CS:start_pc
     *       SS:ESP = FLAT_KERNEL_DS:start_stack
     *          ESI = start_info
     *  [EAX,EBX,ECX,EDX,EDI,EBP are zero]
     *       EFLAGS = IF | 2 (bit 1 is reserved and should always be 1)
     */
    ctxt->user_regs.ds = FLAT_KERNEL_DS;
    ctxt->user_regs.es = FLAT_KERNEL_DS;
    ctxt->user_regs.fs = FLAT_KERNEL_DS;
    ctxt->user_regs.gs = FLAT_KERNEL_DS;
    ctxt->user_regs.ss = FLAT_KERNEL_SS;
    ctxt->user_regs.cs = FLAT_KERNEL_CS;
    ctxt->user_regs.eip = vkern_entry;
    ctxt->user_regs.esp = vstack_start + PAGE_SIZE;
    ctxt->user_regs.esi = vstartinfo_start;
    ctxt->user_regs.eflags = 1 << 9; /* Interrupt Enable */

    ctxt->flags = VGCF_IN_KERNEL;

    /* FPU is set up to default initial state. */
    memset(&ctxt->fpu_ctxt, 0, sizeof(ctxt->fpu_ctxt));

    /* Virtual IDT is empty at start-of-day. */
    for ( i = 0; i < 256; i++ )
    {
        ctxt->trap_ctxt[i].vector = i;
        ctxt->trap_ctxt[i].cs     = FLAT_KERNEL_CS;
    }

    /* No LDT. */
    ctxt->ldt_ents = 0;

    /* Use the default Xen-provided GDT. */
    ctxt->gdt_ents = 0;

    /* Ring 1 stack is the initial stack. */
    ctxt->kernel_ss = FLAT_KERNEL_SS;
    ctxt->kernel_sp = vstack_start + PAGE_SIZE;

    /* No debugging. */
    memset(ctxt->debugreg, 0, sizeof(ctxt->debugreg));

    /* No callback handlers. */
#if defined(__i386__)
    ctxt->event_callback_cs     = FLAT_KERNEL_CS;
    ctxt->event_callback_eip    = 0;
    ctxt->failsafe_callback_cs  = FLAT_KERNEL_CS;
    ctxt->failsafe_callback_eip = 0;
#elif defined(__x86_64__)
    ctxt->event_callback_eip    = 0;
    ctxt->failsafe_callback_eip = 0;
    ctxt->syscall_callback_eip  = 0;
#endif
#endif /* x86 */

    memset( &launch_domctl, 0, sizeof(launch_domctl) );

    launch_domctl.domain = (domid_t)domid;
    launch_domctl.u.vcpucontext.vcpu   = 0;
    set_xen_guest_handle(launch_domctl.u.vcpucontext.ctxt, ctxt);

    launch_domctl.cmd = XEN_DOMCTL_setvcpucontext;
    rc = xc_domctl(xc_handle, &launch_domctl);

    return rc;

 error_out:
    return -1;
}

int xc_linux_build_mem(int xc_handle,
                       uint32_t domid,
                       const char *image_buffer,
                       unsigned long image_size,
                       const char *initrd,
                       unsigned long initrd_len,
                       const char *cmdline,
                       const char *features,
                       unsigned long flags,
                       unsigned int store_evtchn,
                       unsigned long *store_mfn,
                       unsigned int console_evtchn,
                       unsigned long *console_mfn)
{
    int            sts;
    char          *img_buf;
    unsigned long  img_len;
    struct initrd_info initrd_info = { .type = INITRD_none };

    /* A kernel buffer is required */
    if ( (image_buffer == NULL) || (image_size == 0) )
    {
        ERROR("kernel image buffer not present");
        return -1;
    }

    /* If it's gzipped, inflate it;  otherwise, use as is */
    /* xc_inflate_buffer may return the same buffer pointer if */
    /* the buffer is already inflated */
    img_buf = xc_inflate_buffer(image_buffer, image_size, &img_len);
    if ( img_buf == NULL )
    {
        ERROR("unable to inflate kernel image buffer");
        return -1;
    }

    /* RAM disks are optional; if we get one, inflate it */
    if ( initrd != NULL )
    {
        initrd_info.type = INITRD_mem;
        initrd_info.u.mem_addr = xc_inflate_buffer(
            initrd, initrd_len, &initrd_info.len);
        if ( initrd_info.u.mem_addr == NULL )
        {
            ERROR("unable to inflate ram disk buffer");
            sts = -1;
            goto out;
        }
    }

    sts = xc_linux_build_internal(xc_handle, domid, img_buf, img_len,
                                  &initrd_info, cmdline, features, flags,
                                  store_evtchn, store_mfn,
                                  console_evtchn, console_mfn);

 out:
    /* The inflation routines may pass back the same buffer so be */
    /* sure that we have a buffer and that it's not the one passed in. */
    /* Don't unnecessarily annoy/surprise/confound the caller */
    if ( (img_buf != NULL) && (img_buf != image_buffer) )
        free(img_buf);
    if ( (initrd_info.u.mem_addr != NULL) &&
         (initrd_info.u.mem_addr != initrd) )
        free(initrd_info.u.mem_addr);

    return sts;
}

int xc_linux_build(int xc_handle,
                   uint32_t domid,
                   const char *image_name,
                   const char *initrd_name,
                   const char *cmdline,
                   const char *features,
                   unsigned long flags,
                   unsigned int store_evtchn,
                   unsigned long *store_mfn,
                   unsigned int console_evtchn,
                   unsigned long *console_mfn)
{
    char *image = NULL;
    unsigned long image_size;
    struct initrd_info initrd_info = { .type = INITRD_none };
    int fd = -1, sts = -1;

    if ( (image_name == NULL) ||
         ((image = xc_read_image(image_name, &image_size)) == NULL ))
        return -1;

    if ( (initrd_name != NULL) && (strlen(initrd_name) != 0) )
    {
        initrd_info.type = INITRD_file;

        if ( (fd = open(initrd_name, O_RDONLY)) < 0 )
        {
            PERROR("Could not open the initial ramdisk image");
            goto error_out;
        }

        initrd_info.len = xc_get_filesz(fd);
        if ( (initrd_info.u.file_handle = gzdopen(fd, "rb")) == NULL )
        {
            PERROR("Could not allocate decompression state for initrd");
            goto error_out;
        }
    }

    sts = xc_linux_build_internal(xc_handle, domid, image, image_size,
                                  &initrd_info, cmdline, features, flags,
                                  store_evtchn, store_mfn,
                                  console_evtchn, console_mfn);

 error_out:
    free(image);
    if ( initrd_info.type == INITRD_file && initrd_info.u.file_handle )
        gzclose(initrd_info.u.file_handle);
    else if ( fd >= 0 )
        close(fd);

    return sts;
}

/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */