1275970Scy/**
2275970Scy * \file
3275970Scy * \brief Print pmap helper
4275970Scy */
5275970Scy
6275970Scy/*
7275970Scy * Copyright (c) 2012, 2015, ETH Zurich.
8275970Scy * All rights reserved.
9275970Scy *
10275970Scy * This file is distributed under the terms in the attached LICENSE file.
11275970Scy * If you do not find this file, copies can be found by writing to:
12275970Scy * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
13275970Scy */
14275970Scy
15275970Scy
16275970Scy#include <barrelfish/barrelfish.h>
17275970Scy#include <barrelfish/vregion.h>
18275970Scy#include "vspace_dump.h"
19275970Scy#include <stdio.h>
20#include <string.h>
21
22#ifdef TARGET_X86_64_BARRELFISH_PMAP_H
23static int cmp_dump_info(const void *arg1, const void *arg2)
24{
25    struct pmap_dump_info *info1, *info2;
26    info1 = (struct pmap_dump_info *)arg1;
27    info2 = (struct pmap_dump_info *)arg2;
28
29    if (info1->pml4_index < info2->pml4_index)
30        return -1;
31    if (info1->pml4_index > info2->pml4_index)
32        return 1;
33
34    // pml indices equal
35
36    if (info1->pdpt_index < info2->pdpt_index)
37        return -1;
38    if (info1->pdpt_index > info2->pdpt_index)
39        return 1;
40
41    // pdpt indices equal
42
43    if (info1->pdir_index < info2->pdir_index)
44        return -1;
45    if (info1->pdir_index > info2->pdir_index)
46        return 1;
47
48    // pdir indices equal
49
50    if (info1->pt_index < info2->pt_index)
51        return -1;
52    if (info1->pt_index > info2->pt_index)
53        return 1;
54
55    // pt indices equal
56    return 0;
57}
58#elif defined(TARGET_X86_32_BARRELFISH_PMAP_H)
59static int cmp_dump_info(const void *arg1, const void *arg2)
60{
61    struct pmap_dump_info *info1, *info2;
62    info1 = (struct pmap_dump_info *)arg1;
63    info2 = (struct pmap_dump_info *)arg2;
64
65#if CONFIG_PAE
66    if (info1->pdpt_index < info2->pdpt_index)
67        return -1;
68    if (info1->pdpt_index > info2->pdpt_index)
69        return 1;
70
71    // pdpt indices equal
72#endif
73
74    if (info1->pdir_index < info2->pdir_index)
75        return -1;
76    if (info1->pdir_index > info2->pdir_index)
77        return 1;
78
79    // pdir indices equal
80
81    if (info1->pt_index < info2->pt_index)
82        return -1;
83    if (info1->pt_index > info2->pt_index)
84        return 1;
85
86    // pt indices equal
87    return 0;
88}
89#else
90static int cmp_dump_info(const void *arg1, const void *arg2)
91{
92	return 0;
93}
94#endif
95
96#define BUFSIZE 8192
97void dump_pmap(struct pmap *pmap)
98{
99    struct pmap_dump_info *buf = calloc(BUFSIZE, sizeof(struct pmap_dump_info));
100    size_t items_written;
101
102    pmap->f.dump(pmap, buf, BUFSIZE, &items_written);
103
104    printf("items_written=%zd\n", items_written);
105
106    qsort(buf, items_written, sizeof(struct pmap_dump_info), cmp_dump_info);
107
108    for (size_t i = 0; i < items_written; i++) {
109        struct pmap_dump_info *info = buf+i;
110        struct frame_identity fi;
111        frame_identify(info->cap, &fi);
112        printf(PRIfmtPTIDX": 0x%"PRIxGENPADDR", 0x%"PRIxGENVADDR", 0x%zx\n",
113                    GET_PTIDX(info),
114                    fi.base, info->offset, fi.bytes);
115    }
116    printf("\n");
117
118}
119