Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Page cache for QEMU |
| 3 | * The cache is base on a hash of the page address |
| 4 | * |
| 5 | * Copyright 2012 Red Hat, Inc. and/or its affiliates |
| 6 | * |
| 7 | * Authors: |
| 8 | * Orit Wasserman <owasserm@redhat.com> |
| 9 | * |
| 10 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 11 | * See the COPYING file in the top-level directory. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #ifndef PAGE_CACHE_H |
| 16 | #define PAGE_CACHE_H |
| 17 | |
| 18 | /* Page cache for storing guest pages */ |
| 19 | typedef struct PageCache PageCache; |
| 20 | |
| 21 | /** |
| 22 | * cache_init: Initialize the page cache |
| 23 | * |
| 24 | * |
| 25 | * Returns new allocated cache or NULL on error |
| 26 | * |
Juan Quintela | 80f8dfd | 2017-10-06 22:30:45 +0200 | [diff] [blame] | 27 | * @cache_size: cache size in bytes |
Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 28 | * @page_size: cache page size |
Juan Quintela | 80f8dfd | 2017-10-06 22:30:45 +0200 | [diff] [blame] | 29 | * @errp: set *errp if the check failed, with reason |
Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 30 | */ |
Markus Armbruster | 8b9407a | 2021-02-02 15:17:32 +0100 | [diff] [blame] | 31 | PageCache *cache_init(uint64_t cache_size, size_t page_size, Error **errp); |
Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 32 | /** |
| 33 | * cache_fini: free all cache resources |
| 34 | * @cache pointer to the PageCache struct |
| 35 | */ |
| 36 | void cache_fini(PageCache *cache); |
| 37 | |
| 38 | /** |
| 39 | * cache_is_cached: Checks to see if the page is cached |
| 40 | * |
| 41 | * Returns %true if page is cached |
| 42 | * |
| 43 | * @cache pointer to the PageCache struct |
| 44 | * @addr: page addr |
ChenLiang | 27af7d6 | 2014-11-24 19:55:47 +0800 | [diff] [blame] | 45 | * @current_age: current bitmap generation |
Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 46 | */ |
ChenLiang | 27af7d6 | 2014-11-24 19:55:47 +0800 | [diff] [blame] | 47 | bool cache_is_cached(const PageCache *cache, uint64_t addr, |
| 48 | uint64_t current_age); |
Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * get_cached_data: Get the data cached for an addr |
| 52 | * |
| 53 | * Returns pointer to the data cached or NULL if not cached |
| 54 | * |
| 55 | * @cache pointer to the PageCache struct |
| 56 | * @addr: page addr |
| 57 | */ |
| 58 | uint8_t *get_cached_data(const PageCache *cache, uint64_t addr); |
| 59 | |
| 60 | /** |
Peter Lieven | ee0b44a | 2013-02-25 19:12:04 +0200 | [diff] [blame] | 61 | * cache_insert: insert the page into the cache. the page cache |
| 62 | * will dup the data on insert. the previous value will be overwritten |
Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 63 | * |
ChenLiang | 27af7d6 | 2014-11-24 19:55:47 +0800 | [diff] [blame] | 64 | * Returns -1 when the page isn't inserted into cache |
Orit Wasserman | 89db998 | 2014-01-30 20:08:38 +0200 | [diff] [blame] | 65 | * |
Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 66 | * @cache pointer to the PageCache struct |
| 67 | * @addr: page address |
| 68 | * @pdata: pointer to the page |
ChenLiang | 27af7d6 | 2014-11-24 19:55:47 +0800 | [diff] [blame] | 69 | * @current_age: current bitmap generation |
Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 70 | */ |
ChenLiang | 27af7d6 | 2014-11-24 19:55:47 +0800 | [diff] [blame] | 71 | int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata, |
| 72 | uint64_t current_age); |
Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 73 | |
Orit Wasserman | 9fb2664 | 2012-08-06 21:42:50 +0300 | [diff] [blame] | 74 | #endif |