174fe6c29SRuslan Bukin /*
2*85f87cf4SRuslan Bukin * Copyright (c) 2013-2019, Intel Corporation
374fe6c29SRuslan Bukin *
474fe6c29SRuslan Bukin * Redistribution and use in source and binary forms, with or without
574fe6c29SRuslan Bukin * modification, are permitted provided that the following conditions are met:
674fe6c29SRuslan Bukin *
774fe6c29SRuslan Bukin * * Redistributions of source code must retain the above copyright notice,
874fe6c29SRuslan Bukin * this list of conditions and the following disclaimer.
974fe6c29SRuslan Bukin * * Redistributions in binary form must reproduce the above copyright notice,
1074fe6c29SRuslan Bukin * this list of conditions and the following disclaimer in the documentation
1174fe6c29SRuslan Bukin * and/or other materials provided with the distribution.
1274fe6c29SRuslan Bukin * * Neither the name of Intel Corporation nor the names of its contributors
1374fe6c29SRuslan Bukin * may be used to endorse or promote products derived from this software
1474fe6c29SRuslan Bukin * without specific prior written permission.
1574fe6c29SRuslan Bukin *
1674fe6c29SRuslan Bukin * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1774fe6c29SRuslan Bukin * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1874fe6c29SRuslan Bukin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1974fe6c29SRuslan Bukin * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2074fe6c29SRuslan Bukin * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2174fe6c29SRuslan Bukin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2274fe6c29SRuslan Bukin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2374fe6c29SRuslan Bukin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2474fe6c29SRuslan Bukin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2574fe6c29SRuslan Bukin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2674fe6c29SRuslan Bukin * POSSIBILITY OF SUCH DAMAGE.
2774fe6c29SRuslan Bukin */
2874fe6c29SRuslan Bukin
2974fe6c29SRuslan Bukin #include "pt_section.h"
3074fe6c29SRuslan Bukin #include "pt_section_file.h"
3174fe6c29SRuslan Bukin
3274fe6c29SRuslan Bukin #include "intel-pt.h"
3374fe6c29SRuslan Bukin
3474fe6c29SRuslan Bukin #include <stdlib.h>
3574fe6c29SRuslan Bukin #include <string.h>
3674fe6c29SRuslan Bukin
3774fe6c29SRuslan Bukin
fmap_init(struct pt_sec_file_mapping * mapping)3874fe6c29SRuslan Bukin static int fmap_init(struct pt_sec_file_mapping *mapping)
3974fe6c29SRuslan Bukin {
4074fe6c29SRuslan Bukin if (!mapping)
4174fe6c29SRuslan Bukin return -pte_internal;
4274fe6c29SRuslan Bukin
4374fe6c29SRuslan Bukin memset(mapping, 0, sizeof(*mapping));
4474fe6c29SRuslan Bukin
4574fe6c29SRuslan Bukin #if defined(FEATURE_THREADS)
4674fe6c29SRuslan Bukin {
4774fe6c29SRuslan Bukin int errcode;
4874fe6c29SRuslan Bukin
4974fe6c29SRuslan Bukin errcode = mtx_init(&mapping->lock, mtx_plain);
5074fe6c29SRuslan Bukin if (errcode != thrd_success)
5174fe6c29SRuslan Bukin return -pte_bad_lock;
5274fe6c29SRuslan Bukin }
5374fe6c29SRuslan Bukin #endif /* defined(FEATURE_THREADS) */
5474fe6c29SRuslan Bukin
5574fe6c29SRuslan Bukin return 0;
5674fe6c29SRuslan Bukin }
5774fe6c29SRuslan Bukin
fmap_fini(struct pt_sec_file_mapping * mapping)5874fe6c29SRuslan Bukin static void fmap_fini(struct pt_sec_file_mapping *mapping)
5974fe6c29SRuslan Bukin {
6074fe6c29SRuslan Bukin if (!mapping)
6174fe6c29SRuslan Bukin return;
6274fe6c29SRuslan Bukin
6374fe6c29SRuslan Bukin fclose(mapping->file);
6474fe6c29SRuslan Bukin
6574fe6c29SRuslan Bukin #if defined(FEATURE_THREADS)
6674fe6c29SRuslan Bukin
6774fe6c29SRuslan Bukin mtx_destroy(&mapping->lock);
6874fe6c29SRuslan Bukin
6974fe6c29SRuslan Bukin #endif /* defined(FEATURE_THREADS) */
7074fe6c29SRuslan Bukin }
7174fe6c29SRuslan Bukin
fmap_lock(struct pt_sec_file_mapping * mapping)7274fe6c29SRuslan Bukin static int fmap_lock(struct pt_sec_file_mapping *mapping)
7374fe6c29SRuslan Bukin {
7474fe6c29SRuslan Bukin if (!mapping)
7574fe6c29SRuslan Bukin return -pte_internal;
7674fe6c29SRuslan Bukin
7774fe6c29SRuslan Bukin #if defined(FEATURE_THREADS)
7874fe6c29SRuslan Bukin {
7974fe6c29SRuslan Bukin int errcode;
8074fe6c29SRuslan Bukin
8174fe6c29SRuslan Bukin errcode = mtx_lock(&mapping->lock);
8274fe6c29SRuslan Bukin if (errcode != thrd_success)
8374fe6c29SRuslan Bukin return -pte_bad_lock;
8474fe6c29SRuslan Bukin }
8574fe6c29SRuslan Bukin #endif /* defined(FEATURE_THREADS) */
8674fe6c29SRuslan Bukin
8774fe6c29SRuslan Bukin return 0;
8874fe6c29SRuslan Bukin }
8974fe6c29SRuslan Bukin
fmap_unlock(struct pt_sec_file_mapping * mapping)9074fe6c29SRuslan Bukin static int fmap_unlock(struct pt_sec_file_mapping *mapping)
9174fe6c29SRuslan Bukin {
9274fe6c29SRuslan Bukin if (!mapping)
9374fe6c29SRuslan Bukin return -pte_internal;
9474fe6c29SRuslan Bukin
9574fe6c29SRuslan Bukin #if defined(FEATURE_THREADS)
9674fe6c29SRuslan Bukin {
9774fe6c29SRuslan Bukin int errcode;
9874fe6c29SRuslan Bukin
9974fe6c29SRuslan Bukin errcode = mtx_unlock(&mapping->lock);
10074fe6c29SRuslan Bukin if (errcode != thrd_success)
10174fe6c29SRuslan Bukin return -pte_bad_lock;
10274fe6c29SRuslan Bukin }
10374fe6c29SRuslan Bukin #endif /* defined(FEATURE_THREADS) */
10474fe6c29SRuslan Bukin
10574fe6c29SRuslan Bukin return 0;
10674fe6c29SRuslan Bukin }
10774fe6c29SRuslan Bukin
pt_sec_file_map(struct pt_section * section,FILE * file)10874fe6c29SRuslan Bukin int pt_sec_file_map(struct pt_section *section, FILE *file)
10974fe6c29SRuslan Bukin {
11074fe6c29SRuslan Bukin struct pt_sec_file_mapping *mapping;
11174fe6c29SRuslan Bukin uint64_t offset, size;
11274fe6c29SRuslan Bukin long begin, end, fsize;
11374fe6c29SRuslan Bukin int errcode;
11474fe6c29SRuslan Bukin
11574fe6c29SRuslan Bukin if (!section)
11674fe6c29SRuslan Bukin return -pte_internal;
11774fe6c29SRuslan Bukin
11874fe6c29SRuslan Bukin mapping = section->mapping;
11974fe6c29SRuslan Bukin if (mapping)
12074fe6c29SRuslan Bukin return -pte_internal;
12174fe6c29SRuslan Bukin
12274fe6c29SRuslan Bukin offset = section->offset;
12374fe6c29SRuslan Bukin size = section->size;
12474fe6c29SRuslan Bukin
12574fe6c29SRuslan Bukin begin = (long) offset;
12674fe6c29SRuslan Bukin end = begin + (long) size;
12774fe6c29SRuslan Bukin
12874fe6c29SRuslan Bukin /* Check for overflows. */
12974fe6c29SRuslan Bukin if ((uint64_t) begin != offset)
13074fe6c29SRuslan Bukin return -pte_bad_image;
13174fe6c29SRuslan Bukin
13274fe6c29SRuslan Bukin if ((uint64_t) end != (offset + size))
13374fe6c29SRuslan Bukin return -pte_bad_image;
13474fe6c29SRuslan Bukin
13574fe6c29SRuslan Bukin if (end < begin)
13674fe6c29SRuslan Bukin return -pte_bad_image;
13774fe6c29SRuslan Bukin
13874fe6c29SRuslan Bukin /* Validate that the section lies within the file. */
13974fe6c29SRuslan Bukin errcode = fseek(file, 0, SEEK_END);
14074fe6c29SRuslan Bukin if (errcode)
14174fe6c29SRuslan Bukin return -pte_bad_image;
14274fe6c29SRuslan Bukin
14374fe6c29SRuslan Bukin fsize = ftell(file);
14474fe6c29SRuslan Bukin if (fsize < 0)
14574fe6c29SRuslan Bukin return -pte_bad_image;
14674fe6c29SRuslan Bukin
14774fe6c29SRuslan Bukin if (fsize < end)
14874fe6c29SRuslan Bukin return -pte_bad_image;
14974fe6c29SRuslan Bukin
15074fe6c29SRuslan Bukin mapping = malloc(sizeof(*mapping));
15174fe6c29SRuslan Bukin if (!mapping)
15274fe6c29SRuslan Bukin return -pte_nomem;
15374fe6c29SRuslan Bukin
15474fe6c29SRuslan Bukin errcode = fmap_init(mapping);
15574fe6c29SRuslan Bukin if (errcode < 0)
15674fe6c29SRuslan Bukin goto out_mem;
15774fe6c29SRuslan Bukin
15874fe6c29SRuslan Bukin mapping->file = file;
15974fe6c29SRuslan Bukin mapping->begin = begin;
16074fe6c29SRuslan Bukin mapping->end = end;
16174fe6c29SRuslan Bukin
16274fe6c29SRuslan Bukin section->mapping = mapping;
16374fe6c29SRuslan Bukin section->unmap = pt_sec_file_unmap;
16474fe6c29SRuslan Bukin section->read = pt_sec_file_read;
16574fe6c29SRuslan Bukin section->memsize = pt_sec_file_memsize;
16674fe6c29SRuslan Bukin
16774fe6c29SRuslan Bukin return 0;
16874fe6c29SRuslan Bukin
16974fe6c29SRuslan Bukin out_mem:
17074fe6c29SRuslan Bukin free(mapping);
17174fe6c29SRuslan Bukin return errcode;
17274fe6c29SRuslan Bukin }
17374fe6c29SRuslan Bukin
pt_sec_file_unmap(struct pt_section * section)17474fe6c29SRuslan Bukin int pt_sec_file_unmap(struct pt_section *section)
17574fe6c29SRuslan Bukin {
17674fe6c29SRuslan Bukin struct pt_sec_file_mapping *mapping;
17774fe6c29SRuslan Bukin
17874fe6c29SRuslan Bukin if (!section)
17974fe6c29SRuslan Bukin return -pte_internal;
18074fe6c29SRuslan Bukin
18174fe6c29SRuslan Bukin mapping = section->mapping;
18274fe6c29SRuslan Bukin
18374fe6c29SRuslan Bukin if (!mapping || !section->unmap || !section->read || !section->memsize)
18474fe6c29SRuslan Bukin return -pte_internal;
18574fe6c29SRuslan Bukin
18674fe6c29SRuslan Bukin section->mapping = NULL;
18774fe6c29SRuslan Bukin section->unmap = NULL;
18874fe6c29SRuslan Bukin section->read = NULL;
18974fe6c29SRuslan Bukin section->memsize = NULL;
19074fe6c29SRuslan Bukin
19174fe6c29SRuslan Bukin fmap_fini(mapping);
19274fe6c29SRuslan Bukin free(mapping);
19374fe6c29SRuslan Bukin
19474fe6c29SRuslan Bukin return 0;
19574fe6c29SRuslan Bukin }
19674fe6c29SRuslan Bukin
pt_sec_file_read(const struct pt_section * section,uint8_t * buffer,uint16_t size,uint64_t offset)19774fe6c29SRuslan Bukin int pt_sec_file_read(const struct pt_section *section, uint8_t *buffer,
19874fe6c29SRuslan Bukin uint16_t size, uint64_t offset)
19974fe6c29SRuslan Bukin {
20074fe6c29SRuslan Bukin struct pt_sec_file_mapping *mapping;
20174fe6c29SRuslan Bukin FILE *file;
20274fe6c29SRuslan Bukin long begin;
20374fe6c29SRuslan Bukin size_t read;
20474fe6c29SRuslan Bukin int errcode;
20574fe6c29SRuslan Bukin
20674fe6c29SRuslan Bukin if (!buffer || !section)
20774fe6c29SRuslan Bukin return -pte_internal;
20874fe6c29SRuslan Bukin
20974fe6c29SRuslan Bukin mapping = section->mapping;
21074fe6c29SRuslan Bukin if (!mapping)
21174fe6c29SRuslan Bukin return -pte_internal;
21274fe6c29SRuslan Bukin
21374fe6c29SRuslan Bukin file = mapping->file;
21474fe6c29SRuslan Bukin
21574fe6c29SRuslan Bukin /* We already checked in pt_section_read() that the requested memory
21674fe6c29SRuslan Bukin * lies within the section's boundaries.
21774fe6c29SRuslan Bukin *
21874fe6c29SRuslan Bukin * And we checked that the file covers the entire section in
21974fe6c29SRuslan Bukin * pt_sec_file_map(). There's no need to check for overflows, again.
22074fe6c29SRuslan Bukin */
22174fe6c29SRuslan Bukin begin = mapping->begin + (long) offset;
22274fe6c29SRuslan Bukin
22374fe6c29SRuslan Bukin errcode = fmap_lock(mapping);
22474fe6c29SRuslan Bukin if (errcode < 0)
22574fe6c29SRuslan Bukin return errcode;
22674fe6c29SRuslan Bukin
22774fe6c29SRuslan Bukin errcode = fseek(file, begin, SEEK_SET);
22874fe6c29SRuslan Bukin if (errcode)
22974fe6c29SRuslan Bukin goto out_unlock;
23074fe6c29SRuslan Bukin
23174fe6c29SRuslan Bukin read = fread(buffer, 1, size, file);
23274fe6c29SRuslan Bukin
23374fe6c29SRuslan Bukin errcode = fmap_unlock(mapping);
23474fe6c29SRuslan Bukin if (errcode < 0)
23574fe6c29SRuslan Bukin return errcode;
23674fe6c29SRuslan Bukin
23774fe6c29SRuslan Bukin return (int) read;
23874fe6c29SRuslan Bukin
23974fe6c29SRuslan Bukin out_unlock:
24074fe6c29SRuslan Bukin (void) fmap_unlock(mapping);
24174fe6c29SRuslan Bukin return -pte_nomap;
24274fe6c29SRuslan Bukin }
24374fe6c29SRuslan Bukin
pt_sec_file_memsize(const struct pt_section * section,uint64_t * size)24474fe6c29SRuslan Bukin int pt_sec_file_memsize(const struct pt_section *section, uint64_t *size)
24574fe6c29SRuslan Bukin {
24674fe6c29SRuslan Bukin if (!section || !size)
24774fe6c29SRuslan Bukin return -pte_internal;
24874fe6c29SRuslan Bukin
24974fe6c29SRuslan Bukin if (!section->mapping)
25074fe6c29SRuslan Bukin return -pte_internal;
25174fe6c29SRuslan Bukin
25274fe6c29SRuslan Bukin *size = 0ull;
25374fe6c29SRuslan Bukin
25474fe6c29SRuslan Bukin return 0;
25574fe6c29SRuslan Bukin }
256