lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * linux/ipc/shm_ctrl.c |
| 4 | * Copyright (C) 1992, 1993 Krishna Balasubramanian |
| 5 | * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994. |
| 6 | * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli. |
| 7 | * |
| 8 | * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com> |
| 9 | * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com> |
| 10 | * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com> |
| 11 | * |
| 12 | * Better ipc lock (kern_ipc_perm.lock) handling |
| 13 | * Davidlohr Bueso <davidlohr.bueso@hp.com>, June 2013. |
| 14 | */ |
| 15 | #include <linux/mm.h> |
| 16 | #include <asm/pgtable.h> |
| 17 | #include "shm_ctrl.h" |
| 18 | #include "../mm/internal.h" |
| 19 | |
| 20 | /** |
| 21 | * ºê¶¨Òå |
| 22 | */ |
| 23 | #define SHM_UNIT_BUFF_ORDER (12) |
| 24 | #define SHM_KEYS_STATUS_LEN (4*1024) |
| 25 | #define SHM_REMOTE_BUFF_LEN (128*1024) |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 26 | #define SHM_POSIX_HASH_CHARS (26) |
| 27 | #define SHM_POSIX_HASH_BASE (62) |
| 28 | #define SHM_POSIX_HASH_MASK (0x7FF) |
| 29 | |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 30 | #define SHM_BUFF_BASE_PHY_ADDR (g_shm_phyAddr) |
| 31 | |
| 32 | #define SHM_UNIT_BUFF_SIZE (1UL<<SHM_UNIT_BUFF_ORDER) /*4KB*/ |
| 33 | #define SHM_UNIT_INDEX(addr) (((unsigned long)addr - SHM_BUFF_BASE_PHY_ADDR) >> SHM_UNIT_BUFF_ORDER) |
| 34 | #define SHM_UNIT_PAGE_ADDR(index) ((void *)(SHM_BUFF_BASE_PHY_ADDR + ((unsigned long)index << SHM_UNIT_BUFF_ORDER))) |
| 35 | #define SHM_UNIT_NUM_BITS (SHM_REMOTE_BUFF_LEN >> SHM_UNIT_BUFF_ORDER) |
| 36 | #define SHM_CTRL_BITMAP_NUM (SHM_UNIT_NUM_BITS / SHM_CTRL_LONG_32BIT) |
| 37 | |
| 38 | struct shm_key_node { |
| 39 | key_t key; |
| 40 | unsigned int vma_count; |
| 41 | DECLARE_BITMAP(shm_inuse_index, SHM_UNIT_NUM_BITS); |
| 42 | }; |
| 43 | |
| 44 | struct shm_entity { |
| 45 | DECLARE_BITMAP(shm_regions_bitmap, SHM_UNIT_NUM_BITS);/*×ÜÄÚ´æ³Ø¹ÜÀíÐÅÏ¢*/ |
| 46 | struct shm_key_node keys_info_head[SHM_UNIT_NUM_BITS]; /*ÿ¸öshmµÄkey¹ÜÀíÐÅÏ¢*/ |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * È«¾Ö±äÁ¿¶¨Òå |
| 51 | */ |
| 52 | phys_addr_t g_shm_phyAddr = 0; |
| 53 | void *g_shm_region = NULL; |
| 54 | |
| 55 | struct shm_entity *shm_remote_manager; |
| 56 | |
| 57 | /******************************************************************************* |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 58 | * ¹¦ÄÜÃèÊö: shm_hash_name_to_key |
| 59 | * ²ÎÊý˵Ã÷: |
| 60 | * (´«Èë²ÎÊý) name: ¹²ÏíÄÚ´æÃû³Æ |
| 61 | * (´«Èë²ÎÊý) len: ¹²ÏíÄÚ´æÃû³Æ³¤¶È |
| 62 | * (´«³ö²ÎÊý) ÎÞ |
| 63 | * ·µ »Ø Öµ: |
| 64 | * ÆäËü˵Ã÷: This function is used for calc hash value of the name(-2048~-4096) |
| 65 | *******************************************************************************/ |
| 66 | key_t shm_hash_name_to_key(const char *name, int len) |
| 67 | { |
| 68 | int i = 0; |
| 69 | key_t tmp_key = 0; |
| 70 | key_t hash_key = 0; |
| 71 | unsigned long long id = 0; |
| 72 | |
| 73 | for (; i < len; i++) |
| 74 | { |
| 75 | if (name[i] >= 'A' && name[i] <= 'Z') |
| 76 | id = id*SHM_POSIX_HASH_BASE + name[i]-'A'; |
| 77 | else if (name[i] >= 'a' && name[i] <= 'z') |
| 78 | id = id*SHM_POSIX_HASH_BASE + name[i]-'a' + SHM_POSIX_HASH_CHARS; |
| 79 | else if (name[i] >= '0' && name[i] <= '9') |
| 80 | id = id*SHM_POSIX_HASH_BASE + name[i]-'0' + 2*SHM_POSIX_HASH_CHARS; |
| 81 | } |
| 82 | tmp_key =(id & SHM_POSIX_HASH_MASK) + (SHM_POSIX_HASH_MASK + 1); |
| 83 | hash_key = ~tmp_key + 1; |
| 84 | return hash_key; |
| 85 | } |
| 86 | |
| 87 | /******************************************************************************* |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 88 | * ¹¦ÄÜÃèÊö: shm_quary_keyArray |
| 89 | * ²ÎÊý˵Ã÷: |
| 90 | * (´«Èë²ÎÊý) void |
| 91 | * (´«³ö²ÎÊý) void |
| 92 | * ·µ »Ø Öµ: SHM_CTRL_OK or SHM_CTRL_ERROR |
| 93 | * ÆäËü˵Ã÷: This function is used for search a special key in array |
| 94 | *******************************************************************************/ |
| 95 | static int shm_quary_keyArray(const key_t key) |
| 96 | { |
| 97 | unsigned int index = 0; |
| 98 | struct shm_key_node *shm_data = NULL; |
| 99 | |
| 100 | shm_data = shm_remote_manager->keys_info_head; |
| 101 | |
| 102 | for (; index < SHM_UNIT_NUM_BITS; index++) |
| 103 | { |
| 104 | if (shm_data[index].key == key) |
| 105 | return index; |
| 106 | } |
| 107 | return SHM_CTRL_ERROR; |
| 108 | } |
| 109 | |
| 110 | /******************************************************************************* |
| 111 | * ¹¦ÄÜÃèÊö: shm_ctrl_pte_range |
| 112 | * ²ÎÊý˵Ã÷: |
| 113 | * (´«Èë²ÎÊý) void |
| 114 | * (´«³ö²ÎÊý) void |
| 115 | * ·µ »Ø Öµ: SHM_CTRL_OK or SHM_CTRL_ERROR |
| 116 | * ÆäËü˵Ã÷: This function is used for clear the pagetable pte |
| 117 | *******************************************************************************/ |
| 118 | unsigned long shm_ctrl_pte_range(struct mm_struct *mm, |
| 119 | struct vm_area_struct *vma, pmd_t *pmd, |
| 120 | unsigned long addr, unsigned long end) |
| 121 | { |
| 122 | spinlock_t *ptl; |
| 123 | pte_t *start_pte; |
| 124 | pte_t *pte; |
| 125 | |
| 126 | start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl); |
| 127 | pte = start_pte; |
| 128 | arch_enter_lazy_mmu_mode(); |
| 129 | do { |
| 130 | pte_t ptent = *pte; |
| 131 | if (pte_none(ptent)) { |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | if (pte_present(ptent)) { |
| 136 | |
| 137 | ptent = ptep_get_and_clear(mm, addr, pte); |
| 138 | } |
| 139 | pte_clear_not_present_full(mm, addr, pte, 0); |
| 140 | } while (pte++, addr += PAGE_SIZE, addr != end); |
| 141 | |
| 142 | arch_leave_lazy_mmu_mode(); |
| 143 | pte_unmap_unlock(start_pte, ptl); |
| 144 | |
| 145 | return addr; |
| 146 | } |
| 147 | |
| 148 | /******************************************************************************* |
| 149 | * ¹¦ÄÜÃèÊö: shm_ctrl_pmd_range |
| 150 | * ²ÎÊý˵Ã÷: |
| 151 | * (´«Èë²ÎÊý) mm: ÈÎÎñµÄÄÚ´æÃèÊö·û |
| 152 | * (´«Èë²ÎÊý) vma£º¿çºË¹²ÏíÄÚ´æ½ø³ÌµØÖ·¿Õ¼ävma |
| 153 | * (´«Èë²ÎÊý) pud£ºpudÒ³ÉϲãĿ¼ |
| 154 | * (´«Èë²ÎÊý) addr: ÐéÄâÆðʼµØÖ· |
| 155 | * (´«Èë²ÎÊý) end: ÐéÄâ½áÊøµØÖ· |
| 156 | * (´«³ö²ÎÊý) ¿Õ |
| 157 | * ·µ »Ø Öµ: addr |
| 158 | * ÆäËü˵Ã÷: This function is used for clear the pagetable pte |
| 159 | *******************************************************************************/ |
| 160 | static inline unsigned long shm_ctrl_pmd_range(struct mm_struct *mm, |
| 161 | struct vm_area_struct *vma, pud_t *pud, |
| 162 | unsigned long addr, unsigned long end) |
| 163 | { |
| 164 | pmd_t *pmd; |
| 165 | unsigned long next; |
| 166 | |
| 167 | pmd = pmd_offset(pud, addr); |
| 168 | do { |
| 169 | next = pmd_addr_end(addr, end); |
| 170 | /* |
| 171 | * Here there can be other concurrent MADV_DONTNEED or |
| 172 | * trans huge page faults running, and if the pmd is |
| 173 | * none or trans huge it can change under us. This is |
| 174 | * because MADV_DONTNEED holds the mmap_sem in read |
| 175 | * mode. |
| 176 | */ |
| 177 | if (pmd_none_or_trans_huge_or_clear_bad(pmd)) |
| 178 | goto next; |
| 179 | next = shm_ctrl_pte_range(mm, vma, pmd, addr, next); |
| 180 | next: |
| 181 | cond_resched(); |
| 182 | } while (pmd++, addr = next, addr != end); |
| 183 | |
| 184 | return addr; |
| 185 | } |
| 186 | /******************************************************************************* |
| 187 | * ¹¦ÄÜÃèÊö: shm_ctrl_pud_range |
| 188 | * ²ÎÊý˵Ã÷: |
| 189 | * (´«Èë²ÎÊý) mm: ÈÎÎñµÄÄÚ´æÃèÊö·û |
| 190 | * (´«Èë²ÎÊý) vma: ¿çºË¹²ÏíÄÚ´æ½ø³ÌµØÖ·¿Õ¼ävma |
| 191 | * (´«Èë²ÎÊý) pgd: pgdҳĿ¼Ïî |
| 192 | * (´«Èë²ÎÊý) addr: ÐéÄâÆðʼµØÖ· |
| 193 | * (´«Èë²ÎÊý) end: ÐéÄâ½áÊøµØÖ· |
| 194 | * (´«³ö²ÎÊý) ÎÞ |
| 195 | * ·µ »Ø Öµ: SHM_CTRL_OK or SHM_CTRL_ERROR |
| 196 | * ÆäËü˵Ã÷: This function is used for find pud |
| 197 | *******************************************************************************/ |
| 198 | static inline unsigned long shm_ctrl_pud_range(struct mm_struct *mm, |
| 199 | struct vm_area_struct *vma, pgd_t *pgd, |
| 200 | unsigned long addr, unsigned long end) |
| 201 | { |
| 202 | pud_t *pud; |
| 203 | unsigned long next; |
| 204 | |
| 205 | pud = pud_offset(pgd, addr); |
| 206 | do { |
| 207 | next = pud_addr_end(addr, end); |
| 208 | if (pud_none_or_clear_bad(pud)) |
| 209 | continue; |
| 210 | next = shm_ctrl_pmd_range(mm, vma, pud, addr, next); |
| 211 | } while (pud++, addr = next, addr != end); |
| 212 | |
| 213 | return addr; |
| 214 | } |
| 215 | |
| 216 | /******************************************************************************* |
| 217 | * ¹¦ÄÜÃèÊö: shm_unmap_page_range |
| 218 | * ²ÎÊý˵Ã÷: |
| 219 | * (´«Èë²ÎÊý) mm: ÈÎÎñµÄÄÚ´æÃèÊö·û |
| 220 | * (´«Èë²ÎÊý) vma ¿çºË¹²ÏíÄÚ´æ½ø³ÌµØÖ·¿Õ¼ävma |
| 221 | * (´«Èë²ÎÊý) addr ÐéÄâÆðʼµØÖ· |
| 222 | * (´«Èë²ÎÊý) end ÐéÄâ½áÊøµØÖ· |
| 223 | * (´«³ö²ÎÊý) ¿Õ |
| 224 | * ·µ »Ø Öµ: void |
| 225 | * ÆäËü˵Ã÷: This function is used for unmap the shm memory |
| 226 | *******************************************************************************/ |
| 227 | void shm_unmap_page_range(struct mm_struct *mm, struct vm_area_struct *vma, |
| 228 | unsigned long addr, unsigned long end) |
| 229 | { |
| 230 | pgd_t *pgd; |
| 231 | unsigned long next; |
| 232 | |
| 233 | BUG_ON(addr >= end); |
| 234 | pgd = pgd_offset(vma->vm_mm, addr); |
| 235 | do { |
| 236 | next = pgd_addr_end(addr, end); |
| 237 | if (pgd_none_or_clear_bad(pgd)) |
| 238 | continue; |
| 239 | next = shm_ctrl_pud_range(mm, vma, pgd, addr, next); |
| 240 | } while (pgd++, addr = next, addr != end); |
| 241 | } |
| 242 | |
| 243 | /******************************************************************************* |
| 244 | * ¹¦ÄÜÃèÊö: shm_vma_write_pagetable |
| 245 | * ²ÎÊý˵Ã÷: |
| 246 | * (´«Èë²ÎÊý) void |
| 247 | * (´«³ö²ÎÊý) void |
| 248 | * ·µ »Ø Öµ: SHM_CTRL_OK or SHM_CTRL_ERROR |
| 249 | * ÆäËü˵Ã÷: This function is used for create pagetable for shm mem region |
| 250 | *******************************************************************************/ |
| 251 | static int shm_vma_write_pagetable(struct vm_area_struct *vma, unsigned long vm_addr, |
| 252 | phys_addr_t shmaddr_phy) |
| 253 | { |
| 254 | pte_t *pte; |
| 255 | int retval = 0; |
| 256 | pte_t pte_val = 0; |
| 257 | spinlock_t *ptl; |
| 258 | |
| 259 | if (vm_addr < vma->vm_start || vm_addr >= vma->vm_end) |
| 260 | return -EFAULT; |
| 261 | |
| 262 | pte = get_locked_pte(vma->vm_mm, vm_addr, &ptl); |
| 263 | if ((!pte) || (!pte_none(*pte))) |
| 264 | return -EFAULT; |
| 265 | |
| 266 | pte_val = __pte((phys_addr_t)(shmaddr_phy) | pgprot_val(vma->vm_page_prot)); |
| 267 | |
| 268 | set_pte_at(vma->vm_mm, vm_addr, pte, pte_val); |
| 269 | pte_unmap_unlock(pte, ptl); |
| 270 | |
| 271 | return retval; |
| 272 | } |
| 273 | |
| 274 | /******************************************************************************* |
| 275 | * ¹¦ÄÜÃèÊö: shm_fill_keytable |
| 276 | * ²ÎÊý˵Ã÷: |
| 277 | * (´«Èë²ÎÊý) void |
| 278 | * (´«³ö²ÎÊý) void |
| 279 | * ·µ »Ø Öµ: SHM_CTRL_OK or SHM_CTRL_ERROR |
| 280 | * ÆäËü˵Ã÷: This function is used for record the key and index relation |
| 281 | *******************************************************************************/ |
| 282 | int shm_fill_keytable(struct shm_key_node *keydata) |
| 283 | { |
| 284 | unsigned int key_index = 0; |
| 285 | |
| 286 | if (keydata == NULL) |
| 287 | return SHM_CTRL_ERROR; |
| 288 | |
| 289 | for(; key_index < SHM_UNIT_NUM_BITS; key_index++) |
| 290 | { |
| 291 | if(shm_remote_manager->keys_info_head[key_index].key == 0) |
| 292 | { |
| 293 | memcpy(&shm_remote_manager->keys_info_head[key_index], keydata, sizeof(struct shm_key_node)); |
| 294 | return SHM_CTRL_OK; |
| 295 | } |
| 296 | } |
| 297 | return SHM_CTRL_ERROR; |
| 298 | } |
| 299 | |
| 300 | /******************************************************************************* |
| 301 | * ¹¦ÄÜÃèÊö: shm_remove_keynode |
| 302 | * ²ÎÊý˵Ã÷: |
| 303 | * (´«Èë²ÎÊý) void |
| 304 | * (´«³ö²ÎÊý) void |
| 305 | * ·µ »Ø Öµ: SHM_CTRL_OK or SHM_CTRL_ERROR |
| 306 | * ÆäËü˵Ã÷: This function is used for remove the key and index relation |
| 307 | *******************************************************************************/ |
| 308 | static void shm_remove_keynode(unsigned int key_index) |
| 309 | { |
| 310 | memset(&shm_remote_manager->keys_info_head[key_index], 0, sizeof(struct shm_key_node)); |
| 311 | } |
| 312 | |
| 313 | /******************************************************************************* |
| 314 | * ¹¦ÄÜÃèÊö: shm_alloc_new_page |
| 315 | * ²ÎÊý˵Ã÷: |
| 316 | * (´«Èë²ÎÊý) void |
| 317 | * (´«³ö²ÎÊý) void |
| 318 | * ·µ »Ø Öµ: SHM_CTRL_OK or SHM_CTRL_ERROR |
| 319 | * ÆäËü˵Ã÷: This function is used for alloc page from shm mem region |
| 320 | *******************************************************************************/ |
| 321 | int shm_alloc_new_page(struct vm_area_struct *vma, key_t key) |
| 322 | { |
| 323 | unsigned long vm_addr = 0; |
| 324 | unsigned int region_index = 0; |
| 325 | void *new_page = NULL; |
| 326 | struct shm_key_node new_key = {0}; |
| 327 | |
| 328 | if((vma == NULL) || (g_shm_region == NULL)) |
| 329 | { |
| 330 | printk("Shm region is not ready\n"); |
| 331 | return SHM_CTRL_ERROR; |
| 332 | } |
| 333 | |
| 334 | vm_addr = vma->vm_start; |
| 335 | |
| 336 | for (; vm_addr < vma->vm_end; vm_addr += PAGE_SIZE) |
| 337 | { |
| 338 | region_index = find_first_zero_bit(shm_remote_manager->shm_regions_bitmap, SHM_UNIT_NUM_BITS); |
| 339 | |
| 340 | if (region_index < SHM_UNIT_NUM_BITS) |
| 341 | { |
| 342 | set_bit(region_index, shm_remote_manager->shm_regions_bitmap); |
| 343 | new_page = SHM_UNIT_PAGE_ADDR(region_index); |
| 344 | |
| 345 | if (shm_vma_write_pagetable(vma, vm_addr, new_page)) |
| 346 | { |
| 347 | return SHM_CTRL_ERROR; |
| 348 | } |
| 349 | set_bit(region_index, new_key.shm_inuse_index); |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | return SHM_CTRL_ERROR; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if (!bitmap_empty(new_key.shm_inuse_index, SHM_UNIT_NUM_BITS)) |
| 358 | { |
| 359 | new_key.key = key; |
| 360 | new_key.vma_count++; |
| 361 | shm_fill_keytable(&new_key); |
| 362 | } |
| 363 | return SHM_CTRL_OK; |
| 364 | } |
| 365 | |
| 366 | /******************************************************************************* |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 367 | * ¹¦ÄÜÃèÊö: shm_do_newseg_check |
| 368 | * ²ÎÊý˵Ã÷: |
| 369 | * (´«Èë²ÎÊý) void |
| 370 | * (´«³ö²ÎÊý) void |
| 371 | * ·µ »Ø Öµ: SHM_CTRL_OK or SHM_CTRL_ERROR |
| 372 | * ÆäËü˵Ã÷: This function is used for check key and len |
| 373 | *******************************************************************************/ |
| 374 | int shm_do_newseg_check(key_t key, unsigned long len) |
| 375 | { |
| 376 | int ret = 0; |
| 377 | int key_index = 0; |
| 378 | unsigned int shm_weight = 0; |
| 379 | unsigned int shm_pages = 0; |
| 380 | struct shm_key_node *key_node = NULL; |
| 381 | |
| 382 | if(g_shm_region == NULL) |
| 383 | { |
| 384 | printk("shm_do_newsg_check:Shm region is not ready\n"); |
| 385 | return SHM_CTRL_ERROR; |
| 386 | } |
| 387 | soft_spin_lock(SHM_SFLOCK); |
| 388 | |
| 389 | key_index = shm_quary_keyArray(key); |
| 390 | |
| 391 | if (key_index < 0) |
| 392 | { |
| 393 | soft_spin_unlock(SHM_SFLOCK); |
| 394 | return SHM_CTRL_OK; |
| 395 | } |
| 396 | |
| 397 | if ((0 <= key_index) && (key_index < SHM_UNIT_NUM_BITS)) |
| 398 | { |
| 399 | key_node = &shm_remote_manager->keys_info_head[key_index]; |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | soft_spin_unlock(SHM_SFLOCK); |
| 404 | panic("key_index out of range: failed\n"); |
| 405 | } |
| 406 | |
| 407 | shm_pages = PAGE_ALIGN(len) >> PAGE_SHIFT; |
| 408 | shm_weight = bitmap_weight(key_node->shm_inuse_index, SHM_UNIT_NUM_BITS); |
| 409 | soft_spin_unlock(SHM_SFLOCK); |
| 410 | |
| 411 | /*APºÍCAP¹²ÏíÄÚ´æ´óСӦƥÅä*/ |
| 412 | if(shm_weight != shm_pages) |
| 413 | return -EINVAL; |
| 414 | else |
| 415 | return SHM_CTRL_OK; |
| 416 | } |
| 417 | |
| 418 | /******************************************************************************* |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 419 | * ¹¦ÄÜÃèÊö: shm_do_remote_map_vma |
| 420 | * ²ÎÊý˵Ã÷: |
| 421 | * (´«Èë²ÎÊý) void |
| 422 | * (´«³ö²ÎÊý) void |
| 423 | * ·µ »Ø Öµ: SHM_CTRL_OK or SHM_CTRL_ERROR |
| 424 | * ÆäËü˵Ã÷: This function is used for |
| 425 | * |
| 426 | /*²éѯkey,Èç¹ûÒÑ·ÖÅä¹ýʹÓÃkey¶ÔÓ¦µÄbitmap, ·ñÔò´Ó×ÜÄÚ´æ³Ø·ÖÅä |
| 427 | *******************************************************************************/ |
| 428 | int shm_do_remote_map_vma(struct vm_area_struct *vma, key_t key) |
| 429 | { |
| 430 | int ret = 0; |
| 431 | unsigned long vm_addr = 0; |
| 432 | unsigned int region_index = 0; |
| 433 | int key_index = 0; |
| 434 | void *new_page_phy = NULL; |
| 435 | struct shm_key_node *key_node = NULL; |
| 436 | DECLARE_BITMAP(shm_inuse_tmp, SHM_UNIT_NUM_BITS); |
| 437 | |
| 438 | if((vma == NULL) || (g_shm_region == NULL)) |
| 439 | { |
| 440 | printk("shm_do_remote_map_vma:Shm region is not ready\n"); |
| 441 | return SHM_CTRL_ERROR; |
| 442 | } |
| 443 | |
| 444 | /*Ó³ÉävmaΪ·ÇcacheÊôÐÔ*/ |
| 445 | pgprot_noncached(vma->vm_page_prot); |
| 446 | |
| 447 | soft_spin_lock(SHM_SFLOCK); |
| 448 | |
| 449 | key_index = shm_quary_keyArray(key); |
| 450 | |
| 451 | if (key_index < 0) |
| 452 | { |
| 453 | ret = shm_alloc_new_page(vma, key); |
| 454 | soft_spin_unlock(SHM_SFLOCK); |
| 455 | if (ret < 0) |
| 456 | panic("shm_alloc_new_page Fail\n"); |
| 457 | return ret; |
| 458 | } |
| 459 | |
| 460 | vm_addr = vma->vm_start; |
| 461 | |
| 462 | if ((0 <= key_index) && (key_index < SHM_UNIT_NUM_BITS)) |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 463 | { |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 464 | key_node = &shm_remote_manager->keys_info_head[key_index]; |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 465 | } |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 466 | else |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 467 | { |
| 468 | soft_spin_unlock(SHM_SFLOCK); |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 469 | panic("key_index out of range: failed\n"); |
xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 470 | } |
lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 471 | |
| 472 | memcpy(shm_inuse_tmp, key_node->shm_inuse_index, sizeof(shm_inuse_tmp)); |
| 473 | |
| 474 | for (; vm_addr < vma->vm_end; vm_addr += PAGE_SIZE) |
| 475 | { |
| 476 | region_index = find_first_bit(shm_inuse_tmp, SHM_UNIT_NUM_BITS); |
| 477 | if (region_index < SHM_UNIT_NUM_BITS) |
| 478 | { |
| 479 | new_page_phy = SHM_UNIT_PAGE_ADDR(region_index); |
| 480 | if (shm_vma_write_pagetable(vma, vm_addr, new_page_phy)) |
| 481 | { |
| 482 | soft_spin_unlock(SHM_SFLOCK); |
| 483 | panic("shm_do_remote_map_vma vm_insert_page failed\n"); |
| 484 | return SHM_CTRL_ERROR; |
| 485 | } |
| 486 | clear_bit(region_index, shm_inuse_tmp); |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | return SHM_CTRL_ERROR; |
| 491 | } |
| 492 | } |
| 493 | key_node->vma_count++; |
| 494 | |
| 495 | soft_spin_unlock(SHM_SFLOCK); |
| 496 | return SHM_CTRL_OK; |
| 497 | } |
| 498 | |
| 499 | /******************************************************************************* |
| 500 | * ¹¦ÄÜÃèÊö: shm_remote_free_pages |
| 501 | * ²ÎÊý˵Ã÷: |
| 502 | * (´«Èë²ÎÊý) void |
| 503 | * (´«³ö²ÎÊý) void |
| 504 | * ·µ »Ø Öµ: SHM_CTRL_OK or SHM_CTRL_ERROR |
| 505 | * ÆäËü˵Ã÷: This function is used for |
| 506 | *******************************************************************************/ |
| 507 | int shm_remote_free_pages(key_t key) |
| 508 | { |
| 509 | int key_index = 0; |
| 510 | unsigned int region_index = 0; |
| 511 | struct shm_key_node *key_node = NULL; |
| 512 | |
| 513 | if(g_shm_region == NULL) |
| 514 | { |
| 515 | printk("shm_remote_free_pages: Shm region is not ready\n"); |
| 516 | return SHM_CTRL_ERROR; |
| 517 | } |
| 518 | |
| 519 | soft_spin_lock(SHM_SFLOCK); |
| 520 | |
| 521 | /*²éѯkey*/ |
| 522 | key_index = shm_quary_keyArray(key); |
| 523 | if(key_index < 0 || key_index >= SHM_UNIT_NUM_BITS) |
| 524 | { |
| 525 | soft_spin_unlock(SHM_SFLOCK); |
| 526 | panic("error\n"); |
| 527 | } |
| 528 | |
| 529 | /*ÊôÓÚ¿çºËµÄ¾ÍÊͷŵ½³Ø×ÓÀï*/ |
| 530 | key_node = &shm_remote_manager->keys_info_head[key_index]; |
| 531 | |
| 532 | key_node->vma_count--; |
| 533 | |
| 534 | if(key_node->vma_count == 0) |
| 535 | { |
| 536 | for_each_set_bit(region_index, key_node->shm_inuse_index, SHM_UNIT_NUM_BITS) |
| 537 | { |
| 538 | clear_bit(region_index, shm_remote_manager->shm_regions_bitmap); |
| 539 | } |
| 540 | shm_remove_keynode(key_index); |
| 541 | } |
| 542 | soft_spin_unlock(SHM_SFLOCK); |
| 543 | return SHM_CTRL_OK; |
| 544 | } |
| 545 | |
| 546 | /******************************************************************************* |
| 547 | * ¹¦ÄÜÃèÊö: shm_rpcore_init |
| 548 | * ²ÎÊý˵Ã÷: |
| 549 | * (´«Èë²ÎÊý) void |
| 550 | * (´«³ö²ÎÊý) void |
| 551 | * ·µ »Ø Öµ: |
| 552 | * ÆäËü˵Ã÷: This function is used for shm ctrl init |
| 553 | *******************************************************************************/ |
| 554 | static int __init shm_rpcore_init(void) |
| 555 | { |
| 556 | dma_addr_t dma_phys; |
| 557 | dma_addr_t shm_keyInfo_phys; |
| 558 | struct shm_pool_msg shm_msg = {0}; |
| 559 | |
| 560 | g_shm_region = dma_alloc_coherent(NULL, |
| 561 | (size_t)SHM_REMOTE_BUFF_LEN, |
| 562 | &dma_phys, |
| 563 | GFP_KERNEL); |
| 564 | if(!g_shm_region) |
| 565 | { |
| 566 | panic("g_shm_region NOMEM\n"); |
| 567 | } |
| 568 | |
| 569 | g_shm_phyAddr = dma_phys; |
| 570 | |
| 571 | shm_remote_manager = dma_alloc_coherent(NULL, |
| 572 | (size_t)(SHM_KEYS_STATUS_LEN), |
| 573 | &shm_keyInfo_phys, |
| 574 | GFP_KERNEL); |
| 575 | if(!shm_remote_manager) |
| 576 | { |
| 577 | panic("shm_remote_manager NOMEM\n"); |
| 578 | } |
| 579 | |
| 580 | memset(shm_remote_manager, 0, sizeof(struct shm_entity)); |
| 581 | shm_msg.shm_len = SHM_REMOTE_BUFF_LEN; |
| 582 | shm_msg.key_manage_len = SHM_KEYS_STATUS_LEN; |
| 583 | shm_msg.shm_memory_phy = g_shm_phyAddr; |
| 584 | shm_msg.key_manage_phy = shm_keyInfo_phys; |
| 585 | |
| 586 | memcpy((void*)IRAM_BASE_ADDR_SHM_REMOTE_REGION, &shm_msg, sizeof(struct shm_pool_msg)); |
| 587 | |
| 588 | return SHM_CTRL_OK; |
| 589 | |
| 590 | } |
| 591 | |
| 592 | late_initcall(shm_rpcore_init); |
| 593 | |
| 594 | |
| 595 | |