ia64/xen-unstable

view extras/mini-os/gnttab.c @ 13631:db3d03dfe92f

add libelf: an ELF binary parser library.

This patch adds a library with a small collection of helper functions
to parse and load elf binaries. The library handles endianess and
elfsize at runtime.

The patch also shuffles around the include files a bit. Now there is
*one* include file holding all the elf structures
(xen/include/public/elfstructs.h) which is included by everyone who
needs them.

It's dead code with this patch only, putting the code into use happens
in followup patches.

Signed-off-by: Gerd Hoffmann <kraxel@suse.de>
---
tools/libxc/xc_elf.h | 525 -----------------------------------
xen/arch/x86/boot/mkelf32.c | 2
xen/common/Makefile | 2
xen/common/libelf/Makefile | 4
xen/common/libelf/README | 1
xen/common/libelf/libelf-dominfo.c | 420 ++++++++++++++++++++++++++++
xen/common/libelf/libelf-loader.c | 156 ++++++++++
xen/common/libelf/libelf-private.h | 51 +++
xen/common/libelf/libelf-relocate.c | 345 +++++++++++++++++++++++
xen/common/libelf/libelf-tools.c | 225 +++++++++++++++
xen/include/public/elfstructs.h | 527 ++++++++++++++++++++++++++++++++++++
xen/include/public/libelf.h | 238 ++++++++++++++++
xen/include/xen/elf.h | 490 ---------------------------------
13 files changed, 1972 insertions(+), 1014 deletions(-)
author Emmanuel Ackaouy <ack@xensource.com>
date Thu Jan 25 22:16:52 2007 +0000 (2007-01-25)
parents 2406531dae95
children e1364f2c6867
line source
1 /*
2 ****************************************************************************
3 * (C) 2006 - Cambridge University
4 ****************************************************************************
5 *
6 * File: gnttab.c
7 * Author: Steven Smith (sos22@cam.ac.uk)
8 * Changes: Grzegorz Milos (gm281@cam.ac.uk)
9 *
10 * Date: July 2006
11 *
12 * Environment: Xen Minimal OS
13 * Description: Simple grant tables implementation. About as stupid as it's
14 * possible to be and still work.
15 *
16 ****************************************************************************
17 */
18 #include <os.h>
19 #include <mm.h>
20 #include <gnttab.h>
22 #define NR_RESERVED_ENTRIES 8
24 #define NR_GRANT_FRAMES 4
25 #define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(grant_entry_t))
27 static grant_entry_t *gnttab_table;
28 static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
30 static void
31 put_free_entry(grant_ref_t ref)
32 {
33 gnttab_list[ref] = gnttab_list[0];
34 gnttab_list[0] = ref;
36 }
38 static grant_ref_t
39 get_free_entry(void)
40 {
41 unsigned int ref = gnttab_list[0];
42 gnttab_list[0] = gnttab_list[ref];
43 return ref;
44 }
46 grant_ref_t
47 gnttab_grant_access(domid_t domid, unsigned long frame, int readonly)
48 {
49 grant_ref_t ref;
51 ref = get_free_entry();
52 gnttab_table[ref].frame = frame;
53 gnttab_table[ref].domid = domid;
54 wmb();
55 readonly *= GTF_readonly;
56 gnttab_table[ref].flags = GTF_permit_access | readonly;
58 return ref;
59 }
61 grant_ref_t
62 gnttab_grant_transfer(domid_t domid, unsigned long pfn)
63 {
64 grant_ref_t ref;
66 ref = get_free_entry();
67 gnttab_table[ref].frame = pfn;
68 gnttab_table[ref].domid = domid;
69 wmb();
70 gnttab_table[ref].flags = GTF_accept_transfer;
72 return ref;
73 }
75 int
76 gnttab_end_access(grant_ref_t ref)
77 {
78 u16 flags, nflags;
80 nflags = gnttab_table[ref].flags;
81 do {
82 if ((flags = nflags) & (GTF_reading|GTF_writing)) {
83 printk("WARNING: g.e. still in use!\n");
84 return 0;
85 }
86 } while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
87 flags);
89 put_free_entry(ref);
90 return 1;
91 }
93 unsigned long
94 gnttab_end_transfer(grant_ref_t ref)
95 {
96 unsigned long frame;
97 u16 flags;
99 while (!((flags = gnttab_table[ref].flags) & GTF_transfer_committed)) {
100 if (synch_cmpxchg(&gnttab_table[ref].flags, flags, 0) == flags) {
101 printk("Release unused transfer grant.\n");
102 put_free_entry(ref);
103 return 0;
104 }
105 }
107 /* If a transfer is in progress then wait until it is completed. */
108 while (!(flags & GTF_transfer_completed)) {
109 flags = gnttab_table[ref].flags;
110 }
112 /* Read the frame number /after/ reading completion status. */
113 rmb();
114 frame = gnttab_table[ref].frame;
116 put_free_entry(ref);
118 return frame;
119 }
121 grant_ref_t
122 gnttab_alloc_and_grant(void **map)
123 {
124 unsigned long mfn;
125 grant_ref_t gref;
127 *map = (void *)alloc_page();
128 mfn = virt_to_mfn(*map);
129 gref = gnttab_grant_access(0, mfn, 0);
130 return gref;
131 }
133 static const char *gnttabop_error_msgs[] = GNTTABOP_error_msgs;
135 const char *
136 gnttabop_error(int16_t status)
137 {
138 status = -status;
139 if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
140 return "bad status";
141 else
142 return gnttabop_error_msgs[status];
143 }
145 void
146 init_gnttab(void)
147 {
148 struct gnttab_setup_table setup;
149 unsigned long frames[NR_GRANT_FRAMES];
150 int i;
152 for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
153 put_free_entry(i);
155 setup.dom = DOMID_SELF;
156 setup.nr_frames = NR_GRANT_FRAMES;
157 set_xen_guest_handle(setup.frame_list, frames);
159 HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
160 gnttab_table = map_frames(frames, NR_GRANT_FRAMES);
161 printk("gnttab_table mapped at %p.\n", gnttab_table);
162 }