aboutsummaryrefslogtreecommitdiff
path: root/drivers/base/ump/src/common/ump_kernel_core.c
blob: 9d65a1989b4b46e432a140b9a22f29da1a2ec919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
 *
 * (C) COPYRIGHT 2008-2013 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU licence.
 *
 * A copy of the licence is included with the program, and can also be obtained
 * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 */



/* module headers */
#include <linux/ump.h>
#include <linux/ump-ioctl.h>

/* local headers */
#include <common/ump_kernel_core.h>
#include <common/ump_kernel_descriptor_mapping.h>
#include <ump_arch.h>
#include <common/ump_kernel_priv.h>

#define UMP_FLAGS_RANGE ((UMP_PROT_SHAREABLE<<1) - 1u)

static umpp_device device;

ump_result umpp_core_constructor(void)
{
	mutex_init(&device.secure_id_map_lock);
	device.secure_id_map = umpp_descriptor_mapping_create(UMP_EXPECTED_IDS, UMP_MAX_IDS);
	if (NULL != device.secure_id_map)
	{
		if (UMP_OK == umpp_device_initialize())
		{
			return UMP_OK;
		}
		umpp_descriptor_mapping_destroy(device.secure_id_map);
	}
	mutex_destroy(&device.secure_id_map_lock);

	return UMP_ERROR;
}

void umpp_core_destructor(void)
{
	umpp_device_terminate();
	umpp_descriptor_mapping_destroy(device.secure_id_map);
	mutex_destroy(&device.secure_id_map_lock);
}

umpp_session *umpp_core_session_start(void)
{
	umpp_session * session;

	session = kzalloc(sizeof(*session), GFP_KERNEL);
	if (NULL != session)
	{
		mutex_init(&session->session_lock);

		INIT_LIST_HEAD(&session->memory_usage);

		/* try to create import client session, not a failure if they fail to initialize */
		umpp_import_handlers_init(session);
	}

	return session;
}

void umpp_core_session_end(umpp_session *session)
{
	umpp_session_memory_usage * usage, *_usage;
	UMP_ASSERT(session);

	list_for_each_entry_safe(usage, _usage, &session->memory_usage, link)
	{
		printk(KERN_WARNING "UMP: Memory usage cleanup, releasing secure ID %d\n", ump_dd_secure_id_get(usage->mem));
		ump_dd_release(usage->mem);
		kfree(usage);

	}

	/* we should now not hold any imported memory objects,
	 * detatch all import handlers */
	umpp_import_handlers_term(session);

	mutex_destroy(&session->session_lock);
	kfree(session);
}

ump_dd_handle ump_dd_allocate_64(uint64_t size, ump_alloc_flags flags, ump_dd_security_filter filter_func, ump_dd_final_release_callback final_release_func, void* callback_data)
{
	umpp_allocation * alloc;
	int i;

	UMP_ASSERT(size);

	if (flags & (~UMP_FLAGS_RANGE))
	{
		printk(KERN_WARNING "UMP: allocation flags out of allowed bits range\n");
		return UMP_DD_INVALID_MEMORY_HANDLE;
	}

	if( ( flags & (UMP_PROT_CPU_RD | UMP_PROT_W_RD | UMP_PROT_X_RD | UMP_PROT_Y_RD | UMP_PROT_Z_RD ) ) == 0 ||
	    ( flags & (UMP_PROT_CPU_WR | UMP_PROT_W_WR | UMP_PROT_X_WR | UMP_PROT_Y_WR | UMP_PROT_Z_WR )) == 0 )
	{
		printk(KERN_WARNING "UMP: allocation flags should have at least one read and one write permission bit set\n");
		return UMP_DD_INVALID_MEMORY_HANDLE;
	}

	/*check permission flags to be set if hit flags are set too*/
	for (i = UMP_DEVICE_CPU_SHIFT; i<=UMP_DEVICE_Z_SHIFT; i+=4)
	{
		if (flags & (UMP_HINT_DEVICE_RD<<i))
		{
			UMP_ASSERT(flags & (UMP_PROT_DEVICE_RD<<i));
		}
		if (flags & (UMP_HINT_DEVICE_WR<<i))
		{
			UMP_ASSERT(flags & (UMP_PROT_DEVICE_WR<<i));
		}
	}

	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL | __GFP_HARDWALL);

	if (NULL == alloc)
		goto out1;

	alloc->flags = flags;
	alloc->filter_func = filter_func;
	alloc->final_release_func = final_release_func;
	alloc->callback_data = callback_data;
	alloc->size = size;

	mutex_init(&alloc->map_list_lock);
	INIT_LIST_HEAD(&alloc->map_list);
	atomic_set(&alloc->refcount, 1);

#ifdef CONFIG_KDS
	kds_resource_init(&alloc->kds_res);
#endif

	if (!(alloc->flags & UMP_PROT_SHAREABLE))
	{
		alloc->owner = get_current()->pid;
	}

	if (0 != umpp_phys_commit(alloc))
	{
		goto out2;
	}

	/* all set up, allocate an ID for it */

	mutex_lock(&device.secure_id_map_lock);
	alloc->id = umpp_descriptor_mapping_allocate(device.secure_id_map, (void*)alloc);
	mutex_unlock(&device.secure_id_map_lock);

	if ((int)alloc->id == 0)
	{
		/* failed to allocate a secure_id */
		goto out3;
	}

	return alloc;

out3:
	umpp_phys_free(alloc);
out2:
	kfree(alloc);
out1:
	return UMP_DD_INVALID_MEMORY_HANDLE;
}

uint64_t ump_dd_size_get_64(const ump_dd_handle mem)
{
	umpp_allocation * alloc;

	UMP_ASSERT(mem);

	alloc = (umpp_allocation*)mem;

	return alloc->size;
}

/*
 * UMP v1 API
 */
unsigned long ump_dd_size_get(ump_dd_handle mem)
{
	umpp_allocation * alloc;

	UMP_ASSERT(mem);

	alloc = (umpp_allocation*)mem;

	UMP_ASSERT(alloc->flags & UMP_CONSTRAINT_32BIT_ADDRESSABLE);
	UMP_ASSERT(alloc->size <= UMP_UINT32_MAX);

	return (unsigned long)alloc->size;
}

ump_secure_id ump_dd_secure_id_get(const ump_dd_handle mem)
{
	umpp_allocation * alloc;

	UMP_ASSERT(mem);

	alloc = (umpp_allocation*)mem;

	return alloc->id;
}

#ifdef CONFIG_KDS
struct kds_resource * ump_dd_kds_resource_get(const ump_dd_handle mem)
{
	umpp_allocation * alloc;

	UMP_ASSERT(mem);

	alloc = (umpp_allocation*)mem;

	return &alloc->kds_res;
}
#endif

ump_alloc_flags ump_dd_allocation_flags_get(const ump_dd_handle mem)
{
	const umpp_allocation * alloc;

	UMP_ASSERT(mem);
	alloc = (const umpp_allocation *)mem;

	return alloc->flags;
}

ump_dd_handle ump_dd_from_secure_id(ump_secure_id secure_id)
{
	umpp_allocation * alloc = UMP_DD_INVALID_MEMORY_HANDLE;

	mutex_lock(&device.secure_id_map_lock);

	if (0 == umpp_descriptor_mapping_lookup(device.secure_id_map, secure_id, (void**)&alloc))
	{
		if (NULL != alloc->filter_func)
		{
			if (!alloc->filter_func(secure_id, alloc, alloc->callback_data))
			{
				alloc = UMP_DD_INVALID_MEMORY_HANDLE; /* the filter denied access */
			}
		}

		/* check permission to access it */
		if ((UMP_DD_INVALID_MEMORY_HANDLE != alloc) && !(alloc->flags & UMP_PROT_SHAREABLE))
		{
			if (alloc->owner != get_current()->pid)
			{
				alloc = UMP_DD_INVALID_MEMORY_HANDLE; /*no rights for the current process*/
			}
		}

		if (UMP_DD_INVALID_MEMORY_HANDLE != alloc)
		{
			if( ump_dd_retain(alloc) != UMP_DD_SUCCESS)
			{
				alloc = UMP_DD_INVALID_MEMORY_HANDLE;
			}
		}
	}
	mutex_unlock(&device.secure_id_map_lock);

	return alloc;
}

/*
 * UMP v1 API
 */
ump_dd_handle ump_dd_handle_create_from_secure_id(ump_secure_id secure_id)
{
	return ump_dd_from_secure_id(secure_id);
}

int ump_dd_retain(ump_dd_handle mem)
{
	umpp_allocation * alloc;

	UMP_ASSERT(mem);

	alloc = (umpp_allocation*)mem;

	/* check for overflow */
	while(1)
	{
		int refcnt = atomic_read(&alloc->refcount);
		if (refcnt + 1 > 0)
		{
			if(atomic_cmpxchg(&alloc->refcount, refcnt, refcnt + 1) == refcnt)
			{
				return 0;
			}
		}
		else
		{
			return -EBUSY;
		}
	}
}

/*
 * UMP v1 API
 */
void ump_dd_reference_add(ump_dd_handle mem)
{
	ump_dd_retain(mem);
}


void ump_dd_release(ump_dd_handle mem)
{
	umpp_allocation * alloc;
	uint32_t new_cnt;

	UMP_ASSERT(mem);

	alloc = (umpp_allocation*)mem;

	/* secure the id for lookup while releasing */
	mutex_lock(&device.secure_id_map_lock);

	/* do the actual release */
	new_cnt = atomic_sub_return(1, &alloc->refcount);
	if (0 == new_cnt)
	{
		/* remove from the table as this was the last ref */
		umpp_descriptor_mapping_remove(device.secure_id_map, alloc->id);
	}

	/* release the lock as early as possible */
	mutex_unlock(&device.secure_id_map_lock);

	if (0 != new_cnt)
	{
		/* exit if still have refs */
		return;
	}

	UMP_ASSERT(list_empty(&alloc->map_list));

#ifdef CONFIG_KDS
	if (kds_resource_term(&alloc->kds_res))
	{
		printk(KERN_ERR "ump_dd_release: kds_resource_term failed,"
				"unable to release UMP allocation\n");
		return;
	}
#endif
	/* cleanup */
	if (NULL != alloc->final_release_func)
	{
		alloc->final_release_func(alloc, alloc->callback_data);
	}

	if (0 == (alloc->management_flags & UMP_MGMT_EXTERNAL))
	{
		umpp_phys_free(alloc);
	}
	else
	{
		kfree(alloc->block_array);
	}

	mutex_destroy(&alloc->map_list_lock);

	kfree(alloc);
}

/*
 * UMP v1 API
 */
void ump_dd_reference_release(ump_dd_handle mem)
{
	ump_dd_release(mem);
}

void ump_dd_phys_blocks_get_64(const ump_dd_handle mem, uint64_t * const pCount, const ump_dd_physical_block_64 ** const pArray)
{
	const umpp_allocation * alloc;
	UMP_ASSERT(pCount);
	UMP_ASSERT(pArray);
	UMP_ASSERT(mem);
	alloc = (const umpp_allocation *)mem;
	*pCount = alloc->blocksCount;
	*pArray = alloc->block_array;
}

/*
 * UMP v1 API
 */
ump_dd_status_code ump_dd_phys_blocks_get(ump_dd_handle mem, ump_dd_physical_block * const blocks, unsigned long num_blocks)
{
	const umpp_allocation * alloc;
	unsigned long i;
	UMP_ASSERT(mem);
	UMP_ASSERT(blocks);
	UMP_ASSERT(num_blocks);

	alloc = (const umpp_allocation *)mem;

	UMP_ASSERT(alloc->flags & UMP_CONSTRAINT_32BIT_ADDRESSABLE);

	if((uint64_t)num_blocks != alloc->blocksCount)
	{
		return UMP_DD_INVALID;
	}

	for( i = 0; i < num_blocks; i++)
	{
		UMP_ASSERT(alloc->block_array[i].addr <= UMP_UINT32_MAX);
		UMP_ASSERT(alloc->block_array[i].size <= UMP_UINT32_MAX);

		blocks[i].addr = (unsigned long)alloc->block_array[i].addr;
		blocks[i].size = (unsigned long)alloc->block_array[i].size;
	}

	return UMP_DD_SUCCESS;
}
/*
 * UMP v1 API
 */
ump_dd_status_code ump_dd_phys_block_get(ump_dd_handle mem, unsigned long index, ump_dd_physical_block * const block)
{
	const umpp_allocation * alloc;
	UMP_ASSERT(mem);
	UMP_ASSERT(block);
	alloc = (const umpp_allocation *)mem;

	UMP_ASSERT(alloc->flags & UMP_CONSTRAINT_32BIT_ADDRESSABLE);

	UMP_ASSERT(alloc->block_array[index].addr <= UMP_UINT32_MAX);
	UMP_ASSERT(alloc->block_array[index].size <= UMP_UINT32_MAX);

	block->addr = (unsigned long)alloc->block_array[index].addr;
	block->size = (unsigned long)alloc->block_array[index].size;

	return UMP_DD_SUCCESS;
}

/*
 * UMP v1 API
 */
unsigned long ump_dd_phys_block_count_get(ump_dd_handle mem)
{
	const umpp_allocation * alloc;
	UMP_ASSERT(mem);
	alloc = (const umpp_allocation *)mem;

	UMP_ASSERT(alloc->flags & UMP_CONSTRAINT_32BIT_ADDRESSABLE);
	UMP_ASSERT(alloc->blocksCount <= UMP_UINT32_MAX);

	return (unsigned long)alloc->blocksCount;
}

umpp_cpu_mapping * umpp_dd_find_enclosing_mapping(umpp_allocation * alloc, void *uaddr, size_t size)
{
	umpp_cpu_mapping *map;

	void *target_first = uaddr;
	void *target_last = (void*)((uintptr_t)uaddr - 1 + size);

	if (target_last < target_first) /* wrapped */
	{
		return NULL;
	}

	mutex_lock(&alloc->map_list_lock);
	list_for_each_entry(map, &alloc->map_list, link)
	{
		if ( map->vaddr_start <= target_first &&
		   (void*)((uintptr_t)map->vaddr_start + (map->nr_pages << PAGE_SHIFT) - 1) >= target_last)
		{
			goto out;
		}
	}
	map = NULL;
out:
	mutex_unlock(&alloc->map_list_lock);

	return map;
}

void umpp_dd_add_cpu_mapping(umpp_allocation * alloc, umpp_cpu_mapping * map)
{
	UMP_ASSERT(alloc);
	UMP_ASSERT(map);
	mutex_lock(&alloc->map_list_lock);
	list_add(&map->link, &alloc->map_list);
	mutex_unlock(&alloc->map_list_lock);
}

void umpp_dd_remove_cpu_mapping(umpp_allocation * alloc, umpp_cpu_mapping * target)
{
	umpp_cpu_mapping * map;

	UMP_ASSERT(alloc);
	UMP_ASSERT(target);

	mutex_lock(&alloc->map_list_lock);
	list_for_each_entry(map, &alloc->map_list, link)
	{
		if (map == target)
		{
			list_del(&target->link);
			kfree(target);
			mutex_unlock(&alloc->map_list_lock);
			return;
		}
	}

	/* not found, error */
	UMP_ASSERT(0);
}

int umpp_dd_find_start_block(const umpp_allocation * alloc, uint64_t offset, uint64_t * const  block_index, uint64_t * const block_internal_offset)
{
	uint64_t i;

	for (i = 0 ; i < alloc->blocksCount; i++)
	{
		if (offset < alloc->block_array[i].size)
		{
			/* found the block_array element containing this offset */
			*block_index = i;
			*block_internal_offset = offset;
			return 0;
		}
		offset -= alloc->block_array[i].size;
	}

	return -ENXIO;
}

void umpp_dd_cpu_msync_now(ump_dd_handle mem, ump_cpu_msync_op op, void * address, size_t size)
{
	umpp_allocation * alloc;
	void *vaddr;
	umpp_cpu_mapping * mapping;
	uint64_t virt_page_off; /* offset of given address from beginning of the virtual mapping */
	uint64_t phys_page_off; /* offset of the virtual mapping from the beginning of the physical buffer */
	uint64_t page_count; /* number of pages to sync */
	uint64_t i;
	uint64_t block_idx;
	uint64_t block_offset;
	uint64_t paddr;

	UMP_ASSERT((UMP_MSYNC_CLEAN == op) || (UMP_MSYNC_CLEAN_AND_INVALIDATE == op));

	alloc = (umpp_allocation*)mem;
	vaddr = (void*)(uintptr_t)address;

	if((alloc->flags & UMP_CONSTRAINT_UNCACHED) != 0)
	{
		/* mapping is not cached */
		return;
	}

	mapping = umpp_dd_find_enclosing_mapping(alloc, vaddr, size);
	if (NULL == mapping)
	{
		printk(KERN_WARNING "UMP: Illegal cache sync address %lx\n", (uintptr_t)vaddr);
		return; /* invalid pointer or size causes out-of-bounds */
	}

	/* we already know that address + size don't wrap around as umpp_dd_find_enclosing_mapping didn't fail */
	page_count = ((((((uintptr_t)address + size - 1) & PAGE_MASK) - ((uintptr_t)address & PAGE_MASK))) >> PAGE_SHIFT) + 1;
	virt_page_off = (vaddr - mapping->vaddr_start) >> PAGE_SHIFT;
	phys_page_off = mapping->page_off;

	if (umpp_dd_find_start_block(alloc, (virt_page_off + phys_page_off) << PAGE_SHIFT, &block_idx, &block_offset))
	{
		/* should not fail as a valid mapping was found, so the phys mem must exists */
		printk(KERN_WARNING "UMP: Unable to find physical start block with offset %llx\n", virt_page_off + phys_page_off);
		UMP_ASSERT(0);
		return;
	}

	paddr = alloc->block_array[block_idx].addr + block_offset + (((uintptr_t)vaddr) & ((1u << PAGE_SHIFT)-1));

	for (i = 0; i < page_count; i++)
	{
		size_t offset = ((uintptr_t)vaddr) & ((1u << PAGE_SHIFT)-1);
		size_t sz = min((size_t)PAGE_SIZE - offset, size);

		/* check if we've overrrun the current block, if so move to the next block */
		if (paddr >= (alloc->block_array[block_idx].addr + alloc->block_array[block_idx].size))
		{
			block_idx++;
			UMP_ASSERT(block_idx < alloc->blocksCount);
			paddr = alloc->block_array[block_idx].addr;
		}

		if (UMP_MSYNC_CLEAN == op)
		{
			ump_sync_to_memory(paddr, vaddr, sz);
		}
		else /* (UMP_MSYNC_CLEAN_AND_INVALIDATE == op) already validated on entry */
		{
			ump_sync_to_cpu(paddr, vaddr, sz);
		}

		/* advance to next page  */
		vaddr = (void*)((uintptr_t)vaddr + sz);
		size -= sz;
		paddr += sz;
	}
}

UMP_KERNEL_API_EXPORT ump_dd_handle ump_dd_create_from_phys_blocks_64(const ump_dd_physical_block_64 * blocks, uint64_t num_blocks, ump_alloc_flags flags, ump_dd_security_filter filter_func, ump_dd_final_release_callback final_release_func, void* callback_data)
{
	uint64_t size = 0;
	uint64_t i;
	umpp_allocation * alloc;

	UMP_ASSERT(blocks);
	UMP_ASSERT(num_blocks);

	for (i = 0; i < num_blocks; i++)
	{
		size += blocks[i].size;
	}
	UMP_ASSERT(size);

	if (flags & (~UMP_FLAGS_RANGE))
	{
		printk(KERN_WARNING "UMP: allocation flags out of allowed bits range\n");
		return UMP_DD_INVALID_MEMORY_HANDLE;
	}

	if( ( flags & (UMP_PROT_CPU_RD | UMP_PROT_W_RD | UMP_PROT_X_RD | UMP_PROT_Y_RD | UMP_PROT_Z_RD
	    | UMP_PROT_CPU_WR | UMP_PROT_W_WR | UMP_PROT_X_WR | UMP_PROT_Y_WR | UMP_PROT_Z_WR )) == 0 )
	{
		printk(KERN_WARNING "UMP: allocation flags should have at least one read or write permission bit set\n");
		return UMP_DD_INVALID_MEMORY_HANDLE;
	}

	/*check permission flags to be set if hit flags are set too*/
	for (i = UMP_DEVICE_CPU_SHIFT; i<=UMP_DEVICE_Z_SHIFT; i+=4)
	{
		if (flags & (UMP_HINT_DEVICE_RD<<i))
		{
			UMP_ASSERT(flags & (UMP_PROT_DEVICE_RD<<i));
		}
		if (flags & (UMP_HINT_DEVICE_WR<<i))
		{
			UMP_ASSERT(flags & (UMP_PROT_DEVICE_WR<<i));
		}
	}

	alloc = kzalloc(sizeof(*alloc),__GFP_HARDWALL | GFP_KERNEL);

	if (NULL == alloc)
	{
		goto out1;
	}

	alloc->block_array = kzalloc(sizeof(ump_dd_physical_block_64) * num_blocks,__GFP_HARDWALL | GFP_KERNEL);
	if (NULL == alloc->block_array)
	{
		goto out2;
	}

	memcpy(alloc->block_array, blocks, sizeof(ump_dd_physical_block_64) * num_blocks);

#ifdef CONFIG_KDS
	kds_resource_init(&alloc->kds_res);
#endif
	alloc->size = size;
	alloc->blocksCount = num_blocks;
	alloc->flags = flags;
	alloc->filter_func = filter_func;
	alloc->final_release_func = final_release_func;
	alloc->callback_data = callback_data;

	if (!(alloc->flags & UMP_PROT_SHAREABLE))
	{
		alloc->owner = get_current()->pid;
	}

	mutex_init(&alloc->map_list_lock);
	INIT_LIST_HEAD(&alloc->map_list);
	atomic_set(&alloc->refcount, 1);

	/* all set up, allocate an ID */

	mutex_lock(&device.secure_id_map_lock);
	alloc->id = umpp_descriptor_mapping_allocate(device.secure_id_map, (void*)alloc);
	mutex_unlock(&device.secure_id_map_lock);

	if ((int)alloc->id == 0)
	{
		/* failed to allocate a secure_id */
		goto out3;
	}

	alloc->management_flags |= UMP_MGMT_EXTERNAL;

	return alloc;

out3:
	kfree(alloc->block_array);
out2:
	kfree(alloc);
out1:
	return UMP_DD_INVALID_MEMORY_HANDLE;
}


/*
 * UMP v1 API
 */
UMP_KERNEL_API_EXPORT ump_dd_handle ump_dd_handle_create_from_phys_blocks(ump_dd_physical_block * blocks, unsigned long num_blocks)
{
	ump_dd_handle mem;
	ump_dd_physical_block_64 *block_64_array;
	ump_alloc_flags flags = UMP_V1_API_DEFAULT_ALLOCATION_FLAGS;
	unsigned long i;

	UMP_ASSERT(blocks);
	UMP_ASSERT(num_blocks);

	block_64_array = kzalloc(num_blocks * sizeof(*block_64_array), __GFP_HARDWALL | GFP_KERNEL);

	if(block_64_array == NULL)
	{
		return UMP_DD_INVALID_MEMORY_HANDLE;
	}

	/* copy physical blocks */
	for( i = 0; i < num_blocks; i++)
	{
		block_64_array[i].addr = blocks[i].addr;
		block_64_array[i].size = blocks[i].size;
	}

	mem = ump_dd_create_from_phys_blocks_64(block_64_array, num_blocks, flags, NULL, NULL, NULL);

	kfree(block_64_array);

	return mem;

}