@@ -128,22 +128,33 @@ impl Frame {
128
128
}
129
129
130
130
/// allocate contigugous frames, and you can specify the alignment, set the lower `align_log2` bits to 0.
131
- pub fn new_contiguous_with_base (
132
- frame_count : usize ,
133
- align_log2 : usize ,
134
- ) -> HvResult < Self > {
131
+ pub fn new_contiguous_with_base ( frame_count : usize , align_log2 : usize ) -> HvResult < Self > {
132
+ let align_mask = ( 1 << align_log2) - 1 ;
135
133
// Create a vector to keep track of attempted frames
136
134
let mut attempted_frames = Vec :: new ( ) ;
137
- loop {
138
- if let Ok ( frame) = Frame :: new_contiguous ( frame_count, 0 ) {
139
- if frame. start_paddr ( ) & ( ( 1 << align_log2) - 1 ) == 0 {
140
- info ! ( "new contiguous success!!! start_paddr:0x{:x}" , frame. start_paddr( ) ) ;
135
+ loop {
136
+ if let Ok ( frame) = Frame :: new_contiguous ( frame_count, 0 ) {
137
+ if frame. start_paddr ( ) & align_mask == 0 {
138
+ info ! (
139
+ "new contiguous success!!! start_paddr:0x{:x}" ,
140
+ frame. start_paddr( )
141
+ ) ;
141
142
return Ok ( frame) ;
142
- } else {
143
+ } else {
144
+ let start_paddr = frame. start_paddr ( ) ;
145
+ let next_aligned_addr = ( start_paddr + align_mask) & !align_mask;
146
+ let temp_frame_count = ( next_aligned_addr - start_paddr) / PAGE_SIZE ;
143
147
drop ( frame) ;
144
- attempted_frames. push ( Frame :: new_zero ( ) ) ;
148
+ attempted_frames. push ( Frame :: new_contiguous ( temp_frame_count, 0 ) ) ;
149
+ if let Ok ( frame) = Frame :: new_contiguous ( frame_count, 0 ) {
150
+ info ! (
151
+ "new contiguous success!!! start_paddr:0x{:x}" ,
152
+ frame. start_paddr( )
153
+ ) ;
154
+ return Ok ( frame) ;
155
+ }
145
156
}
146
- } else {
157
+ } else {
147
158
return Err ( hv_err ! ( ENOMEM ) ) ;
148
159
}
149
160
}
0 commit comments