ia64/xen-unstable

view tools/libxc/powerpc64/utils.c @ 12976:a510c94ceaa3

[POWERPC] Merge in outstanding changes from xenppc-unstable.hg.
It's a long story, but basically a small divergence in xenppc-unstable meant a
large number of changesets couldn't be directly imported to xen-unstable, so
this changeset includes all of them.
Signed-off-by: Hollis Blanchard <hollisb@us.ibm.com>
author Hollis Blanchard <hollisb@us.ibm.com>
date Tue Dec 12 09:56:06 2006 -0600 (2006-12-12)
parents 95cf2e3e7e0a
children 30af6cfdb05c 09b3fd488726
line source
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 *
16 * Copyright (C) IBM Corporation 2006
17 *
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19 * Jimi Xenidis <jimix@watson.ibm.com>
20 */
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <inttypes.h>
30 #include <xen/xen.h>
31 #include <xen/memory.h>
32 #include <xc_private.h>
33 #include <xg_private.h>
34 #include <xenctrl.h>
36 #include "flatdevtree_env.h"
37 #include "flatdevtree.h"
38 #include "utils.h"
40 unsigned long get_rma_pages(void *devtree)
41 {
42 void *rma;
43 uint64_t rma_reg[2];
44 int rc;
46 rma = ft_find_node(devtree, "/memory@0");
47 if (rma == NULL) {
48 DPRINTF("couldn't find /memory@0\n");
49 return 0;
50 }
51 rc = ft_get_prop(devtree, rma, "reg", rma_reg, sizeof(rma_reg));
52 if (rc < 0) {
53 DPRINTF("couldn't get /memory@0/reg\n");
54 return 0;
55 }
56 if (rma_reg[0] != 0) {
57 DPRINTF("RMA did not start at 0\n");
58 return 0;
59 }
60 return rma_reg[1] >> PAGE_SHIFT;
61 }
63 int get_rma_page_array(int xc_handle, int domid, xen_pfn_t **page_array,
64 unsigned long nr_pages)
65 {
66 int rc;
67 int i;
68 xen_pfn_t *p;
70 *page_array = malloc(nr_pages * sizeof(xen_pfn_t));
71 if (*page_array == NULL) {
72 perror("malloc");
73 return -1;
74 }
76 DPRINTF("xc_get_pfn_list\n");
77 /* We know that the RMA is machine contiguous so lets just get the
78 * first MFN and fill the rest in ourselves */
79 rc = xc_get_pfn_list(xc_handle, domid, *page_array, 1);
80 if (rc == -1) {
81 perror("Could not get the page frame list");
82 return -1;
83 }
84 p = *page_array;
85 for (i = 1; i < nr_pages; i++)
86 p[i] = p[i - 1] + 1;
87 return 0;
88 }
90 int install_image(
91 int xc_handle,
92 int domid,
93 xen_pfn_t *page_array,
94 void *image,
95 unsigned long paddr,
96 unsigned long size)
97 {
98 uint8_t *img = image;
99 int i;
100 int rc = 0;
102 if (paddr & ~PAGE_MASK) {
103 printf("*** unaligned address\n");
104 return -1;
105 }
107 for (i = 0; i < size; i += PAGE_SIZE) {
108 void *page = img + i;
109 xen_pfn_t pfn = (paddr + i) >> PAGE_SHIFT;
110 xen_pfn_t mfn = page_array[pfn];
112 rc = xc_copy_to_domain_page(xc_handle, domid, mfn, page);
113 if (rc < 0) {
114 perror("xc_copy_to_domain_page");
115 break;
116 }
117 }
118 return rc;
119 }
121 void *load_file(const char *path, unsigned long *filesize)
122 {
123 void *img;
124 ssize_t size;
125 int fd;
127 DPRINTF("load_file(%s)\n", path);
129 fd = open(path, O_RDONLY);
130 if (fd < 0) {
131 perror(path);
132 return NULL;
133 }
135 size = lseek(fd, 0, SEEK_END);
136 if (size < 0) {
137 perror(path);
138 close(fd);
139 return NULL;
140 }
141 lseek(fd, 0, SEEK_SET);
143 img = malloc(size);
144 if (img == NULL) {
145 perror(path);
146 close(fd);
147 return NULL;
148 }
150 size = read(fd, img, size);
151 if (size <= 0) {
152 perror(path);
153 close(fd);
154 free(img);
155 return NULL;
156 }
158 if (filesize)
159 *filesize = size;
160 close(fd);
161 return img;
162 }
164 int load_elf_kernel(
165 int xc_handle,
166 int domid,
167 const char *kernel_path,
168 struct domain_setup_info *dsi,
169 xen_pfn_t *page_array)
170 {
171 struct load_funcs load_funcs;
172 char *kernel_img;
173 unsigned long kernel_size;
174 int rc;
176 /* load the kernel ELF file */
177 kernel_img = load_file(kernel_path, &kernel_size);
178 if (kernel_img == NULL) {
179 rc = -1;
180 goto out;
181 }
183 DPRINTF("probe_elf\n");
184 rc = probe_elf(kernel_img, kernel_size, &load_funcs);
185 if (rc < 0) {
186 rc = -1;
187 printf("%s is not an ELF file\n", kernel_path);
188 goto out;
189 }
191 DPRINTF("parseimage\n");
192 rc = (load_funcs.parseimage)(kernel_img, kernel_size, dsi);
193 if (rc < 0) {
194 rc = -1;
195 goto out;
196 }
198 DPRINTF("loadimage\n");
199 (load_funcs.loadimage)(kernel_img, kernel_size, xc_handle, domid,
200 page_array, dsi);
202 DPRINTF(" v_start %016"PRIx64"\n", dsi->v_start);
203 DPRINTF(" v_end %016"PRIx64"\n", dsi->v_end);
204 DPRINTF(" v_kernstart %016"PRIx64"\n", dsi->v_kernstart);
205 DPRINTF(" v_kernend %016"PRIx64"\n", dsi->v_kernend);
206 DPRINTF(" v_kernentry %016"PRIx64"\n", dsi->v_kernentry);
208 out:
209 free(kernel_img);
210 return rc;
211 }