2 * Copyright (c) 2013-2019, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 #include "pt_section.h"
32 #include "pt_image_section_cache.h"
38 static char *dupstr(const char *str)
46 /* Silently truncate the name if it gets too big. */
47 len = strnlen(str, 4096ul);
49 dup = malloc(len + 1);
55 return memcpy(dup, str, len);
58 static struct pt_section_list *pt_mk_section_list(struct pt_section *section,
59 const struct pt_asid *asid,
62 uint64_t size, int isid)
64 struct pt_section_list *list;
67 list = malloc(sizeof(*list));
71 memset(list, 0, sizeof(*list));
73 errcode = pt_section_get(section);
77 pt_msec_init(&list->section, section, asid, vaddr, offset, size);
87 static void pt_section_list_free(struct pt_section_list *list)
92 pt_section_put(list->section.section);
93 pt_msec_fini(&list->section);
97 static void pt_section_list_free_tail(struct pt_section_list *list)
100 struct pt_section_list *trash;
105 pt_section_list_free(trash);
109 void pt_image_init(struct pt_image *image, const char *name)
114 memset(image, 0, sizeof(*image));
116 image->name = dupstr(name);
119 void pt_image_fini(struct pt_image *image)
124 pt_section_list_free_tail(image->sections);
127 memset(image, 0, sizeof(*image));
130 struct pt_image *pt_image_alloc(const char *name)
132 struct pt_image *image;
134 image = malloc(sizeof(*image));
136 pt_image_init(image, name);
141 void pt_image_free(struct pt_image *image)
143 pt_image_fini(image);
147 const char *pt_image_name(const struct pt_image *image)
155 int pt_image_add(struct pt_image *image, struct pt_section *section,
156 const struct pt_asid *asid, uint64_t vaddr, int isid)
158 struct pt_section_list **list, *next, *removed, *new;
159 uint64_t size, begin, end;
162 if (!image || !section)
163 return -pte_internal;
165 size = pt_section_size(section);
169 next = pt_mk_section_list(section, asid, begin, 0ull, size, isid);
176 /* Check for overlaps while we move to the end of the list. */
177 list = &(image->sections);
179 const struct pt_mapped_section *msec;
180 const struct pt_asid *masid;
181 struct pt_section_list *current;
182 struct pt_section *lsec;
183 uint64_t lbegin, lend, loff;
186 msec = ¤t->section;
187 masid = pt_msec_asid(msec);
189 errcode = pt_asid_match(masid, asid);
194 list = &((*list)->next);
198 lbegin = pt_msec_begin(msec);
199 lend = pt_msec_end(msec);
201 if ((end <= lbegin) || (lend <= begin)) {
202 list = &((*list)->next);
206 /* The new section overlaps with @msec's section. */
207 lsec = pt_msec_section(msec);
208 loff = pt_msec_offset(msec);
210 /* We remove @msec and insert new sections for the remaining
211 * parts, if any. Those new sections are not mapped initially
212 * and need to be added to the end of the section list.
214 *list = current->next;
216 /* Keep a list of removed sections so we can re-add them in case
219 current->next = removed;
222 /* Add a section covering the remaining bytes at the front. */
223 if (lbegin < begin) {
224 new = pt_mk_section_list(lsec, masid, lbegin, loff,
225 begin - lbegin, current->isid);
227 errcode = -pte_nomem;
235 /* Add a section covering the remaining bytes at the back. */
237 new = pt_mk_section_list(lsec, masid, end,
238 loff + (end - lbegin),
239 lend - end, current->isid);
241 errcode = -pte_nomem;
251 pt_section_list_free_tail(next);
253 /* Re-add removed sections to the tail of the section list. */
254 for (; *list; list = &((*list)->next))
261 pt_section_list_free_tail(removed);
267 int pt_image_remove(struct pt_image *image, struct pt_section *section,
268 const struct pt_asid *asid, uint64_t vaddr)
270 struct pt_section_list **list;
272 if (!image || !section)
273 return -pte_internal;
275 for (list = &image->sections; *list; list = &((*list)->next)) {
276 struct pt_mapped_section *msec;
277 const struct pt_section *sec;
278 const struct pt_asid *masid;
279 struct pt_section_list *trash;
284 msec = &trash->section;
285 masid = pt_msec_asid(msec);
287 errcode = pt_asid_match(masid, asid);
294 begin = pt_msec_begin(msec);
295 sec = pt_msec_section(msec);
296 if (sec == section && begin == vaddr) {
298 pt_section_list_free(trash);
304 return -pte_bad_image;
307 int pt_image_add_file(struct pt_image *image, const char *filename,
308 uint64_t offset, uint64_t size,
309 const struct pt_asid *uasid, uint64_t vaddr)
311 struct pt_section *section;
315 if (!image || !filename)
318 errcode = pt_asid_from_user(&asid, uasid);
323 errcode = pt_mk_section(§ion, filename, offset, size);
327 errcode = pt_image_add(image, section, &asid, vaddr, 0);
329 (void) pt_section_put(section);
333 /* The image list got its own reference; let's drop ours. */
334 errcode = pt_section_put(section);
341 int pt_image_copy(struct pt_image *image, const struct pt_image *src)
343 struct pt_section_list *list;
349 /* There is nothing to do if we copy an image to itself.
351 * Besides, pt_image_add() may move sections around, which would
352 * interfere with our section iteration.
358 for (list = src->sections; list; list = list->next) {
361 errcode = pt_image_add(image, list->section.section,
372 int pt_image_remove_by_filename(struct pt_image *image, const char *filename,
373 const struct pt_asid *uasid)
375 struct pt_section_list **list;
377 int errcode, removed;
379 if (!image || !filename)
382 errcode = pt_asid_from_user(&asid, uasid);
387 for (list = &image->sections; *list;) {
388 struct pt_mapped_section *msec;
389 const struct pt_section *sec;
390 const struct pt_asid *masid;
391 struct pt_section_list *trash;
395 msec = &trash->section;
396 masid = pt_msec_asid(msec);
398 errcode = pt_asid_match(masid, &asid);
407 sec = pt_msec_section(msec);
408 tname = pt_section_filename(sec);
410 if (tname && (strcmp(tname, filename) == 0)) {
412 pt_section_list_free(trash);
422 int pt_image_remove_by_asid(struct pt_image *image,
423 const struct pt_asid *uasid)
425 struct pt_section_list **list;
427 int errcode, removed;
432 errcode = pt_asid_from_user(&asid, uasid);
437 for (list = &image->sections; *list;) {
438 struct pt_mapped_section *msec;
439 const struct pt_asid *masid;
440 struct pt_section_list *trash;
443 msec = &trash->section;
444 masid = pt_msec_asid(msec);
446 errcode = pt_asid_match(masid, &asid);
456 pt_section_list_free(trash);
464 int pt_image_set_callback(struct pt_image *image,
465 read_memory_callback_t *callback, void *context)
470 image->readmem.callback = callback;
471 image->readmem.context = context;
476 static int pt_image_read_callback(struct pt_image *image, int *isid,
477 uint8_t *buffer, uint16_t size,
478 const struct pt_asid *asid, uint64_t addr)
480 read_memory_callback_t *callback;
483 return -pte_internal;
485 callback = image->readmem.callback;
491 return callback(buffer, size, asid, addr, image->readmem.context);
494 /* Check whether a mapped section contains an address.
496 * Returns zero if @msec contains @vaddr.
497 * Returns a negative error code otherwise.
498 * Returns -pte_nomap if @msec does not contain @vaddr.
500 static inline int pt_image_check_msec(const struct pt_mapped_section *msec,
501 const struct pt_asid *asid,
504 const struct pt_asid *masid;
509 return -pte_internal;
511 begin = pt_msec_begin(msec);
512 end = pt_msec_end(msec);
513 if (vaddr < begin || end <= vaddr)
516 masid = pt_msec_asid(msec);
517 errcode = pt_asid_match(masid, asid);
520 errcode = -pte_nomap;
528 /* Find the section containing a given address in a given address space.
530 * On success, the found section is moved to the front of the section list.
531 * If caching is enabled, maps the section.
533 * Returns zero on success, a negative error code otherwise.
535 static int pt_image_fetch_section(struct pt_image *image,
536 const struct pt_asid *asid, uint64_t vaddr)
538 struct pt_section_list **start, **list;
541 return -pte_internal;
543 start = &image->sections;
544 for (list = start; *list;) {
545 struct pt_mapped_section *msec;
546 struct pt_section_list *elem;
550 msec = &elem->section;
552 errcode = pt_image_check_msec(msec, asid, vaddr);
554 if (errcode != -pte_nomap)
561 /* Move the section to the front if it isn't already. */
574 int pt_image_read(struct pt_image *image, int *isid, uint8_t *buffer,
575 uint16_t size, const struct pt_asid *asid, uint64_t addr)
577 struct pt_mapped_section *msec;
578 struct pt_section_list *slist;
579 struct pt_section *section;
583 return -pte_internal;
585 errcode = pt_image_fetch_section(image, asid, addr);
587 if (errcode != -pte_nomap)
590 return pt_image_read_callback(image, isid, buffer, size, asid,
594 slist = image->sections;
596 return -pte_internal;
599 msec = &slist->section;
601 section = pt_msec_section(msec);
603 errcode = pt_section_map(section);
607 status = pt_msec_read(msec, buffer, size, addr);
609 errcode = pt_section_unmap(section);
614 if (status != -pte_nomap)
617 return pt_image_read_callback(image, isid, buffer, size, asid,
624 int pt_image_add_cached(struct pt_image *image,
625 struct pt_image_section_cache *iscache, int isid,
626 const struct pt_asid *uasid)
628 struct pt_section *section;
633 if (!image || !iscache)
636 errcode = pt_iscache_lookup(iscache, §ion, &vaddr, isid);
640 errcode = pt_asid_from_user(&asid, uasid);
644 status = pt_image_add(image, section, &asid, vaddr, isid);
646 /* We grab a reference when we add the section. Drop the one we
647 * obtained from cache lookup.
649 errcode = pt_section_put(section);
656 int pt_image_find(struct pt_image *image, struct pt_mapped_section *usec,
657 const struct pt_asid *asid, uint64_t vaddr)
659 struct pt_mapped_section *msec;
660 struct pt_section_list *slist;
661 struct pt_section *section;
665 return -pte_internal;
667 errcode = pt_image_fetch_section(image, asid, vaddr);
671 slist = image->sections;
673 return -pte_internal;
675 msec = &slist->section;
676 section = pt_msec_section(msec);
678 errcode = pt_section_get(section);
687 int pt_image_validate(const struct pt_image *image,
688 const struct pt_mapped_section *usec, uint64_t vaddr,
691 const struct pt_section_list *slist;
696 return -pte_internal;
698 /* Check that @vaddr lies within @usec. */
699 begin = pt_msec_begin(usec);
700 end = pt_msec_end(usec);
701 if (vaddr < begin || end <= vaddr)
704 /* We assume that @usec is a copy of the top of our stack and accept
705 * sporadic validation fails if it isn't, e.g. because it has moved
708 * A failed validation requires decoders to re-fetch the section so it
709 * only results in a (relatively small) performance loss.
711 slist = image->sections;
715 if (slist->isid != isid)
718 status = memcmp(&slist->section, usec, sizeof(*usec));