1 /* 2 * Copyright 2008-2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Copyright 2003-2007, Axel Dörfler, axeld@pinc-software.de. 4 * Distributed under the terms of the MIT License. 5 * 6 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. 7 * Distributed under the terms of the NewOS License. 8 */ 9 #ifndef _KERNEL_VM_VM_CACHE_H 10 #define _KERNEL_VM_VM_CACHE_H 11 12 13 #include <debug.h> 14 #include <kernel.h> 15 #include <util/DoublyLinkedList.h> 16 #include <vm/vm.h> 17 #include <vm/vm_types.h> 18 19 #include "kernel_debug_config.h" 20 21 22 struct kernel_args; 23 class ObjectCache; 24 25 26 enum { 27 CACHE_TYPE_RAM = 0, 28 CACHE_TYPE_VNODE, 29 CACHE_TYPE_DEVICE, 30 CACHE_TYPE_NULL 31 }; 32 33 enum { 34 PAGE_EVENT_NOT_BUSY = 0x01 // page not busy anymore 35 }; 36 37 38 extern ObjectCache* gCacheRefObjectCache; 39 extern ObjectCache* gAnonymousCacheObjectCache; 40 extern ObjectCache* gAnonymousNoSwapCacheObjectCache; 41 extern ObjectCache* gVnodeCacheObjectCache; 42 extern ObjectCache* gDeviceCacheObjectCache; 43 extern ObjectCache* gNullCacheObjectCache; 44 45 46 struct VMCachePagesTreeDefinition { 47 typedef page_num_t KeyType; 48 typedef vm_page NodeType; 49 50 static page_num_t GetKey(const NodeType* node) 51 { 52 return node->cache_offset; 53 } 54 55 static SplayTreeLink<NodeType>* GetLink(NodeType* node) 56 { 57 return &node->cache_link; 58 } 59 60 static int Compare(page_num_t key, const NodeType* node) 61 { 62 return key == node->cache_offset ? 0 63 : (key < node->cache_offset ? -1 : 1); 64 } 65 66 static NodeType** GetListLink(NodeType* node) 67 { 68 return &node->cache_next; 69 } 70 }; 71 72 typedef IteratableSplayTree<VMCachePagesTreeDefinition> VMCachePagesTree; 73 74 75 struct VMCache : public DoublyLinkedListLinkImpl<VMCache> { 76 public: 77 typedef DoublyLinkedList<VMCache> ConsumerList; 78 79 public: 80 VMCache(); 81 virtual ~VMCache(); 82 83 status_t Init(uint32 cacheType, uint32 allocationFlags); 84 85 virtual void Delete(); 86 87 inline bool Lock(); 88 inline bool TryLock(); 89 inline bool SwitchLock(mutex* from); 90 inline bool SwitchFromReadLock(rw_lock* from); 91 void Unlock(bool consumerLocked = false); 92 inline void AssertLocked(); 93 94 inline void AcquireRefLocked(); 95 inline void AcquireRef(); 96 inline void ReleaseRefLocked(); 97 inline void ReleaseRef(); 98 inline void ReleaseRefAndUnlock( 99 bool consumerLocked = false); 100 101 inline VMCacheRef* CacheRef() const { return fCacheRef; } 102 103 void WaitForPageEvents(vm_page* page, uint32 events, 104 bool relock); 105 void NotifyPageEvents(vm_page* page, uint32 events) 106 { if (fPageEventWaiters != NULL) 107 _NotifyPageEvents(page, events); } 108 inline void MarkPageUnbusy(vm_page* page); 109 110 vm_page* LookupPage(off_t offset); 111 void InsertPage(vm_page* page, off_t offset); 112 void RemovePage(vm_page* page); 113 void MovePage(vm_page* page); 114 void MoveAllPages(VMCache* fromCache); 115 116 inline page_num_t WiredPagesCount() const; 117 inline void IncrementWiredPagesCount(); 118 inline void DecrementWiredPagesCount(); 119 120 virtual int32 GuardSize() { return 0; } 121 122 void AddConsumer(VMCache* consumer); 123 124 status_t InsertAreaLocked(VMArea* area); 125 status_t RemoveArea(VMArea* area); 126 void TransferAreas(VMCache* fromCache); 127 uint32 CountWritableAreas(VMArea* ignoreArea) const; 128 129 status_t WriteModified(); 130 status_t SetMinimalCommitment(off_t commitment, 131 int priority); 132 virtual status_t Resize(off_t newSize, int priority); 133 134 status_t FlushAndRemoveAllPages(); 135 136 void* UserData() { return fUserData; } 137 void SetUserData(void* data) { fUserData = data; } 138 // Settable by the lock owner and valid as 139 // long as the lock is owned. 140 141 // for debugging only 142 int32 RefCount() const 143 { return fRefCount; } 144 145 // backing store operations 146 virtual status_t Commit(off_t size, int priority); 147 virtual bool HasPage(off_t offset); 148 149 virtual status_t Read(off_t offset, const generic_io_vec *vecs, 150 size_t count,uint32 flags, 151 generic_size_t *_numBytes); 152 virtual status_t Write(off_t offset, const generic_io_vec *vecs, 153 size_t count, uint32 flags, 154 generic_size_t *_numBytes); 155 virtual status_t WriteAsync(off_t offset, 156 const generic_io_vec* vecs, size_t count, 157 generic_size_t numBytes, uint32 flags, 158 AsyncIOCallback* callback); 159 virtual bool CanWritePage(off_t offset); 160 161 virtual int32 MaxPagesPerWrite() const 162 { return -1; } // no restriction 163 virtual int32 MaxPagesPerAsyncWrite() const 164 { return -1; } // no restriction 165 166 virtual status_t Fault(struct VMAddressSpace *aspace, 167 off_t offset); 168 169 virtual void Merge(VMCache* source); 170 171 virtual status_t AcquireUnreferencedStoreRef(); 172 virtual void AcquireStoreRef(); 173 virtual void ReleaseStoreRef(); 174 175 virtual bool DebugHasPage(off_t offset); 176 vm_page* DebugLookupPage(off_t offset); 177 178 virtual void Dump(bool showPages) const; 179 180 protected: 181 virtual void DeleteObject() = 0; 182 183 public: 184 VMArea* areas; 185 ConsumerList consumers; 186 // list of caches that use this cache as a source 187 VMCachePagesTree pages; 188 VMCache* source; 189 off_t virtual_base; 190 off_t virtual_end; 191 off_t committed_size; 192 // TODO: Remove! 193 uint32 page_count; 194 uint32 temporary : 1; 195 uint32 type : 6; 196 197 #if DEBUG_CACHE_LIST 198 VMCache* debug_previous; 199 VMCache* debug_next; 200 #endif 201 202 private: 203 struct PageEventWaiter; 204 friend struct VMCacheRef; 205 206 private: 207 void _NotifyPageEvents(vm_page* page, uint32 events); 208 209 inline bool _IsMergeable() const; 210 211 void _MergeWithOnlyConsumer(); 212 void _RemoveConsumer(VMCache* consumer); 213 214 private: 215 int32 fRefCount; 216 mutex fLock; 217 PageEventWaiter* fPageEventWaiters; 218 void* fUserData; 219 VMCacheRef* fCacheRef; 220 page_num_t fWiredPagesCount; 221 }; 222 223 224 #if DEBUG_CACHE_LIST 225 extern VMCache* gDebugCacheList; 226 #endif 227 228 229 class VMCacheFactory { 230 public: 231 static status_t CreateAnonymousCache(VMCache*& cache, 232 bool canOvercommit, int32 numPrecommittedPages, 233 int32 numGuardPages, bool swappable, 234 int priority); 235 static status_t CreateVnodeCache(VMCache*& cache, 236 struct vnode* vnode); 237 static status_t CreateDeviceCache(VMCache*& cache, 238 addr_t baseAddress); 239 static status_t CreateNullCache(int priority, VMCache*& cache); 240 }; 241 242 243 244 bool 245 VMCache::Lock() 246 { 247 return mutex_lock(&fLock) == B_OK; 248 } 249 250 251 bool 252 VMCache::TryLock() 253 { 254 return mutex_trylock(&fLock) == B_OK; 255 } 256 257 258 bool 259 VMCache::SwitchLock(mutex* from) 260 { 261 return mutex_switch_lock(from, &fLock) == B_OK; 262 } 263 264 265 bool 266 VMCache::SwitchFromReadLock(rw_lock* from) 267 { 268 return mutex_switch_from_read_lock(from, &fLock) == B_OK; 269 } 270 271 272 void 273 VMCache::AssertLocked() 274 { 275 ASSERT_LOCKED_MUTEX(&fLock); 276 } 277 278 279 void 280 VMCache::AcquireRefLocked() 281 { 282 ASSERT_LOCKED_MUTEX(&fLock); 283 284 fRefCount++; 285 } 286 287 288 void 289 VMCache::AcquireRef() 290 { 291 Lock(); 292 fRefCount++; 293 Unlock(); 294 } 295 296 297 void 298 VMCache::ReleaseRefLocked() 299 { 300 ASSERT_LOCKED_MUTEX(&fLock); 301 302 fRefCount--; 303 } 304 305 306 void 307 VMCache::ReleaseRef() 308 { 309 Lock(); 310 fRefCount--; 311 Unlock(); 312 } 313 314 315 void 316 VMCache::ReleaseRefAndUnlock(bool consumerLocked) 317 { 318 ReleaseRefLocked(); 319 Unlock(consumerLocked); 320 } 321 322 323 void 324 VMCache::MarkPageUnbusy(vm_page* page) 325 { 326 ASSERT(page->busy); 327 page->busy = false; 328 NotifyPageEvents(page, PAGE_EVENT_NOT_BUSY); 329 } 330 331 332 page_num_t 333 VMCache::WiredPagesCount() const 334 { 335 return fWiredPagesCount; 336 } 337 338 339 void 340 VMCache::IncrementWiredPagesCount() 341 { 342 ASSERT(fWiredPagesCount < page_count); 343 344 fWiredPagesCount++; 345 } 346 347 348 void 349 VMCache::DecrementWiredPagesCount() 350 { 351 ASSERT(fWiredPagesCount > 0); 352 353 fWiredPagesCount--; 354 } 355 356 357 // vm_page methods implemented here to avoid VMCache.h inclusion in vm_types.h 358 359 inline void 360 vm_page::IncrementWiredCount() 361 { 362 if (fWiredCount++ == 0) 363 cache_ref->cache->IncrementWiredPagesCount(); 364 } 365 366 367 inline void 368 vm_page::DecrementWiredCount() 369 { 370 ASSERT(fWiredCount > 0); 371 372 if (--fWiredCount == 0) 373 cache_ref->cache->DecrementWiredPagesCount(); 374 } 375 376 377 #ifdef __cplusplus 378 extern "C" { 379 #endif 380 381 status_t vm_cache_init(struct kernel_args *args); 382 void vm_cache_init_post_heap(); 383 struct VMCache *vm_cache_acquire_locked_page_cache(struct vm_page *page, 384 bool dontWait); 385 386 #ifdef __cplusplus 387 } 388 #endif 389 390 391 #endif /* _KERNEL_VM_VM_CACHE_H */ 392